svf-tools 1.0.976 → 1.0.978

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,792 +0,0 @@
1
- //===- FSMPTA.cpp -- 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.cpp
25
- *
26
- * Created on: Jul 29, 2015
27
- * Author: Yulei Sui, Peng Di
28
- */
29
-
30
- #include "Util/Options.h"
31
- #include "MTA/FSMPTA.h"
32
- #include "MTA/MHP.h"
33
- #include "MTA/PCG.h"
34
- #include "MemoryModel/PointsTo.h"
35
-
36
- using namespace SVF;
37
- using namespace SVFUtil;
38
-
39
- FSMPTA* FSMPTA::mfspta = nullptr;
40
- u32_t MTASVFGBuilder::numOfNewSVFGEdges = 0;
41
- u32_t MTASVFGBuilder::numOfRemovedSVFGEdges = 0;
42
- u32_t MTASVFGBuilder::numOfRemovedPTS = 0;
43
-
44
- /*!
45
- *
46
- */
47
- void MTASVFGBuilder::buildSVFG()
48
- {
49
- MemSSA* mssa = svfg->getMSSA();
50
- svfg->buildSVFG();
51
- if (ADDEDGE_NOEDGE != Options::AddModelFlag())
52
- {
53
- DBOUT(DGENERAL, outs() << SVFUtil::pasMsg("FSMPTA adding edge\n"));
54
- DBOUT(DMTA, outs() << SVFUtil::pasMsg("FSMPTA adding edge\n"));
55
- connectMHPEdges(mssa->getPTA());
56
- }
57
- if (mssa->getPTA()->printStat())
58
- svfg->performStat();
59
- }
60
-
61
- /*!
62
- *
63
- */
64
- void MTASVFGBuilder::collectLoadStoreSVFGNodes()
65
- {
66
-
67
- for (SVFG::const_iterator it = svfg->begin(), eit = svfg->end(); it != eit; ++it)
68
- {
69
- const SVFGNode* snode = it->second;
70
- if (SVFUtil::isa<LoadSVFGNode>(snode))
71
- {
72
- const StmtSVFGNode* node = SVFUtil::cast<StmtSVFGNode>(snode);
73
- if (node->getInst())
74
- {
75
- ldnodeSet.insert(node);
76
- }
77
- }
78
- if (SVFUtil::isa<StoreSVFGNode>(snode))
79
- {
80
- const StmtSVFGNode* node = SVFUtil::cast<StmtSVFGNode>(snode);
81
- if (node->getInst())
82
- {
83
- stnodeSet.insert(node);
84
- }
85
- }
86
- }
87
- }
88
- bool MTASVFGBuilder::recordEdge(NodeID id1, NodeID id2, PointsTo pts)
89
- {
90
- NodeIDPair pair = std::make_pair(id1, id2);
91
- if (recordedges.find(pair) == recordedges.end())
92
- {
93
- recordedges.insert(pair);
94
- edge2pts[pair] = pts;
95
- return true;
96
- }
97
- else
98
- {
99
- edge2pts[pair] |= pts;
100
- }
101
- return false;
102
- }
103
- bool MTASVFGBuilder::recordAddingEdge(NodeID id1, NodeID id2, PointsTo pts)
104
- {
105
- return recordEdge(id1, id2, pts);
106
- }
107
-
108
- bool MTASVFGBuilder::recordRemovingEdge(NodeID id1, NodeID id2, PointsTo pts)
109
- {
110
- return recordEdge(id1, id2, pts);
111
- }
112
-
113
- void MTASVFGBuilder::performAddingMHPEdges()
114
- {
115
- MTASVFGBuilder::numOfNewSVFGEdges = recordedges.size();
116
- while (!recordedges.empty())
117
- {
118
- std::pair<NodeID, NodeID> edgepair = *recordedges.begin();
119
- PointsTo pts = edge2pts[edgepair];
120
- recordedges.erase(recordedges.begin());
121
- addTDEdges(edgepair.first, edgepair.second, pts);
122
- }
123
- }
124
-
125
- SVFGEdge* MTASVFGBuilder::addTDEdges(NodeID srcId, NodeID dstId, PointsTo& pts)
126
- {
127
-
128
- SVFGNode* srcNode = svfg->getSVFGNode(srcId);
129
- SVFGNode* dstNode = svfg->getSVFGNode(dstId);
130
-
131
- if(SVFGEdge* edge = svfg->hasThreadVFGEdge(srcNode,dstNode,SVFGEdge::TheadMHPIndirectVF))
132
- {
133
- assert(SVFUtil::isa<IndirectSVFGEdge>(edge) && "this should be a indirect value flow edge!");
134
- return (SVFUtil::cast<IndirectSVFGEdge>(edge)->addPointsTo(pts.toNodeBS()) ? edge : nullptr);
135
- }
136
- else
137
- {
138
- MTASVFGBuilder::numOfNewSVFGEdges++;
139
- ThreadMHPIndSVFGEdge* indirectEdge = new ThreadMHPIndSVFGEdge(srcNode,dstNode);
140
- indirectEdge->addPointsTo(pts.toNodeBS());
141
- return (svfg->addSVFGEdge(indirectEdge) ? indirectEdge : nullptr);
142
- }
143
- }
144
-
145
- void MTASVFGBuilder::performRemovingMHPEdges()
146
- {
147
- while (!recordedges.empty())
148
- {
149
- std::pair<NodeID, NodeID> edgepair = *recordedges.begin();
150
- recordedges.erase(recordedges.begin());
151
-
152
- PointsTo remove_pts = edge2pts[edgepair];
153
- const StmtSVFGNode* n1 = SVFUtil::cast<StmtSVFGNode>(svfg->getSVFGNode(edgepair.first));
154
- const StmtSVFGNode* n2 = SVFUtil::cast<StmtSVFGNode>(svfg->getSVFGNode(edgepair.second));
155
-
156
- assert (n1&&n2 && "one node of removed pair is null");
157
- assert (n1->hasOutgoingEdge() && "n1 doesn't have out edge");
158
- assert (n2->hasIncomingEdge() && "n2 doesn't have in edge");
159
-
160
- Set<SVFGEdge*> removededges;
161
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = n1->OutEdgeBegin(); iter != n1->OutEdgeEnd(); ++iter)
162
- {
163
- SVFGEdge* edge = *iter;
164
- if (edge->isIndirectVFGEdge() && (edge->getDstNode()==n2))
165
- {
166
- IndirectSVFGEdge* e = SVFUtil::cast<IndirectSVFGEdge>(edge);
167
- const NodeBS& pts = e->getPointsTo();
168
- for (PointsTo::iterator o = remove_pts.begin(), eo = remove_pts.end(); o != eo; ++o)
169
- {
170
- if (const_cast<NodeBS&>(pts).test(*o))
171
- {
172
- const_cast<NodeBS&>(pts).reset(*o);
173
- MTASVFGBuilder::numOfRemovedPTS ++;
174
- }
175
- }
176
-
177
- if (0 == e->getPointsTo().count())
178
- {
179
- removededges.insert(edge);
180
- }
181
- }
182
- }
183
-
184
- while(!removededges.empty())
185
- {
186
- SVFGEdge* edge = *removededges.begin();
187
- removededges.erase(removededges.begin());
188
- svfg->removeSVFGEdge(edge);
189
- DBOUT(DMTA,outs()<<"Read Precision remove: "<<edge->getSrcID()<<" -> "<<edge->getDstID()<<"\n");
190
- MTASVFGBuilder::numOfRemovedSVFGEdges++;
191
- }
192
- }
193
- }
194
-
195
-
196
- /*!
197
- *
198
- */
199
-
200
- /// whether is a first write in the lock span.
201
- bool MTASVFGBuilder::isHeadofSpan(const StmtSVFGNode* n, LockAnalysis::LockSpan lspan)
202
- {
203
- SVFGNodeLockSpan pair(n,lspan);
204
- if (pairheadmap.find(pair) != pairheadmap.end())
205
- return pairheadmap[pair];
206
-
207
- SVFGNodeIDSet prev = getPrevNodes(n);
208
-
209
- for (SVFGNodeIDSet::iterator it = prev.begin(), eit = prev.end(); it != eit; ++it)
210
- {
211
- assert (SVFUtil::isa<StoreSVFGNode>(svfg->getSVFGNode(*it)) && "prev is not a store node");
212
- const StmtSVFGNode* prevNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
213
- const SVFInstruction* prevIns = prevNode->getInst();
214
-
215
- if (lockana->hasOneCxtInLockSpan(prevIns, lspan))
216
- {
217
- pairheadmap[pair]=false;
218
- return false;
219
- }
220
- }
221
- pairheadmap[pair]=true;
222
- return true;
223
- }
224
-
225
- bool MTASVFGBuilder::isHeadofSpan(const StmtSVFGNode* n, InstSet mergespan)
226
- {
227
-
228
- SVFGNodeIDSet prev = getPrevNodes(n);
229
-
230
- for (SVFGNodeIDSet::iterator it = prev.begin(), eit = prev.end(); it != eit; ++it)
231
- {
232
- assert (SVFUtil::isa<StoreSVFGNode>(svfg->getSVFGNode(*it)) && "prev is not a store node");
233
- const StmtSVFGNode* prevNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
234
- const SVFInstruction* prevIns = prevNode->getInst();
235
- if (mergespan.find(prevIns)!=mergespan.end())
236
- return false;
237
- }
238
- return true;
239
- }
240
-
241
- /// whether for all lockspans that n belongs to, n is the first write.
242
- /// strong constraints but scalable
243
- bool MTASVFGBuilder::isHeadofSpan(const StmtSVFGNode* n)
244
- {
245
- if (headmap.find(n) != headmap.end())
246
- return headmap[n];
247
-
248
- SVFGNodeIDSet prev = getPrevNodes(n);
249
-
250
- for (SVFGNodeIDSet::iterator it = prev.begin(), eit = prev.end(); it != eit; ++it)
251
- {
252
- assert(SVFUtil::isa<StoreSVFGNode>(svfg->getSVFGNode(*it)) && "prev is not a store node");
253
- const StmtSVFGNode* prevNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
254
- const SVFInstruction* prevIns = prevNode->getInst();
255
-
256
- if (lockana->isInSameSpan(prevIns, n->getInst()))
257
- {
258
- headmap[n]=false;
259
- return false;
260
- }
261
- }
262
- headmap[n]=true;
263
- return true;
264
- }
265
-
266
- bool MTASVFGBuilder::isTailofSpan(const StmtSVFGNode* n, InstSet mergespan)
267
- {
268
-
269
- SVFGNodeIDSet succ = getSuccNodes(n);
270
-
271
- for (SVFGNodeIDSet::iterator it = succ.begin(), eit = succ.end(); it != eit; ++it)
272
- {
273
- assert((SVFUtil::isa<StoreSVFGNode, LoadSVFGNode>(svfg->getSVFGNode(*it))) &&
274
- "succ is not a store/load node");
275
- const StmtSVFGNode* succNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
276
- const SVFInstruction* succIns = succNode->getInst();
277
-
278
- if (mergespan.find(succIns)!=mergespan.end())
279
- return false;
280
- }
281
- return true;
282
- }
283
-
284
- /// whether is a last write in the lock span.
285
- bool MTASVFGBuilder::isTailofSpan(const StmtSVFGNode* n, LockAnalysis::LockSpan lspan)
286
- {
287
- assert(SVFUtil::isa<StoreSVFGNode>(n) && "Node is not a store node");
288
-
289
- SVFGNodeLockSpan pair(n,lspan);
290
- if (pairtailmap.find(pair) != pairtailmap.end())
291
- return pairtailmap[pair];
292
-
293
- SVFGNodeIDSet succ = getSuccNodes(n);
294
- for (SVFGNodeIDSet::iterator it = succ.begin(), eit = succ.end(); it != eit; ++it)
295
- {
296
- assert((SVFUtil::isa<StoreSVFGNode, LoadSVFGNode>(svfg->getSVFGNode(*it))) &&
297
- "succ is not a store/load node");
298
- if (SVFUtil::isa<LoadSVFGNode>(svfg->getSVFGNode(*it)))
299
- continue;
300
- const StmtSVFGNode* succNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
301
- const SVFInstruction* succIns = succNode->getInst();
302
-
303
- if (lockana->hasOneCxtInLockSpan(succIns, lspan))
304
- {
305
- pairtailmap[pair]=false;
306
- return false;
307
- }
308
- }
309
- pairtailmap[pair]=true;
310
- return true;
311
- }
312
-
313
-
314
- /// whether for all lockspans that n belongs to, n is the last write.
315
- /// strong constraints but scalable
316
- bool MTASVFGBuilder::isTailofSpan(const StmtSVFGNode* n)
317
- {
318
- assert(SVFUtil::isa<StoreSVFGNode>(n) && "Node is not a store node");
319
-
320
- if (tailmap.find(n) != tailmap.end())
321
- return tailmap[n];
322
-
323
- SVFGNodeIDSet succ = getSuccNodes(n);
324
-
325
- for (SVFGNodeIDSet::iterator it = succ.begin(), eit = succ.end(); it != eit; ++it)
326
- {
327
- assert((SVFUtil::isa<StoreSVFGNode, LoadSVFGNode>(svfg->getSVFGNode(*it))) && "succ is not a store/load node");
328
- if (SVFUtil::isa<LoadSVFGNode>(svfg->getSVFGNode(*it)))
329
- continue;
330
-
331
- const StmtSVFGNode* succNode = SVFUtil::dyn_cast<StmtSVFGNode>(svfg->getSVFGNode(*it));
332
- const SVFInstruction* succIns = succNode->getInst();
333
-
334
- if (lockana->isInSameSpan(succIns, n->getInst()))
335
- {
336
- tailmap[n] = false;
337
- return false;
338
- }
339
- }
340
- tailmap[n]=true;
341
- return true;
342
- }
343
-
344
- MTASVFGBuilder::SVFGNodeIDSet MTASVFGBuilder::getPrevNodes(const StmtSVFGNode* n)
345
- {
346
- if (prevset.find(n)!=prevset.end())
347
- return prevset[n];
348
-
349
- SVFGNodeIDSet prev;
350
- SVFGNodeSet worklist;
351
- SVFGNodeSet visited;
352
-
353
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = n->InEdgeBegin(); iter != n->InEdgeEnd(); ++iter)
354
- {
355
- SVFGEdge* edge = *iter;
356
- if (edge->isIndirectVFGEdge())
357
- {
358
- worklist.insert(edge->getSrcNode());
359
- }
360
- }
361
-
362
- while (!worklist.empty())
363
- {
364
- const SVFGNode* node = *worklist.begin();
365
- worklist.erase(worklist.begin());
366
- visited.insert(node);
367
- if (SVFUtil::isa<StoreSVFGNode>(node))
368
- prev.set(node->getId());
369
- else
370
- {
371
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = node->InEdgeBegin(); iter != node->InEdgeEnd(); ++iter)
372
- {
373
- SVFGEdge* edge = *iter;
374
- if (edge->isIndirectVFGEdge() && visited.find(edge->getSrcNode())==visited.end())
375
- worklist.insert(edge->getSrcNode());
376
- }
377
- }
378
- }
379
- prevset[n]=prev;
380
- return prev;
381
- }
382
- MTASVFGBuilder::SVFGNodeIDSet MTASVFGBuilder::getSuccNodes(const StmtSVFGNode* n)
383
- {
384
- if (succset.find(n)!=succset.end())
385
- return succset[n];
386
-
387
- SVFGNodeIDSet succ;
388
- SVFGNodeSet worklist;
389
- SVFGNodeSet visited;
390
-
391
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = n->OutEdgeBegin(); iter != n->OutEdgeEnd(); ++iter)
392
- {
393
- SVFGEdge* edge = *iter;
394
- if (edge->isIndirectVFGEdge())
395
- {
396
- worklist.insert(edge->getDstNode());
397
- }
398
- }
399
-
400
- while (!worklist.empty())
401
- {
402
- const SVFGNode* node = *worklist.begin();
403
- worklist.erase(worklist.begin());
404
- visited.insert(node);
405
- if (SVFUtil::isa<StoreSVFGNode, LoadSVFGNode>(node))
406
- succ.set(node->getId());
407
- else
408
- {
409
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = node->OutEdgeBegin(); iter != node->OutEdgeEnd(); ++iter)
410
- {
411
- SVFGEdge* edge = *iter;
412
- if (edge->isIndirectVFGEdge() && visited.find(edge->getDstNode()) == visited.end())
413
- worklist.insert(edge->getDstNode());
414
- }
415
- }
416
- }
417
- succset[n]=succ;
418
- return succ;
419
- }
420
- MTASVFGBuilder::SVFGNodeIDSet MTASVFGBuilder::getSuccNodes(const StmtSVFGNode* n, NodeID o)
421
- {
422
-
423
- SVFGNodeIDSet succ;
424
- SVFGNodeSet worklist;
425
- SVFGNodeSet visited;
426
-
427
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = n->OutEdgeBegin(); iter != n->OutEdgeEnd(); ++iter)
428
- {
429
- SVFGEdge* edge = *iter;
430
- if (edge->isIndirectVFGEdge())
431
- {
432
- IndirectSVFGEdge* e = SVFUtil::cast<IndirectSVFGEdge>(edge);
433
- NodeBS pts = e->getPointsTo();
434
- if(pts.test(o))
435
- worklist.insert(edge->getDstNode());
436
- }
437
- }
438
-
439
- while (!worklist.empty())
440
- {
441
- const SVFGNode* node = *worklist.begin();
442
- worklist.erase(worklist.begin());
443
- visited.insert(node);
444
- if (SVFUtil::isa<StoreSVFGNode, LoadSVFGNode>(node))
445
- succ.set(node->getId());
446
- else
447
- {
448
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = node->OutEdgeBegin(); iter != node->OutEdgeEnd(); ++iter)
449
- {
450
- SVFGEdge* edge = *iter;
451
- if (edge->isIndirectVFGEdge() && visited.find(edge->getDstNode()) == visited.end())
452
- {
453
- IndirectSVFGEdge* e = SVFUtil::cast<IndirectSVFGEdge>(edge);
454
- NodeBS pts = e->getPointsTo();
455
- if(pts.test(o))
456
- worklist.insert(edge->getDstNode());
457
- }
458
- }
459
- }
460
- }
461
-
462
- return succ;
463
- }
464
-
465
- void MTASVFGBuilder::handleStoreLoadNonSparse(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
466
- {
467
- PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
468
- pts &= pta->getPts(n2->getPAGSrcNodeID());
469
-
470
- addTDEdges(n1->getId(), n2->getId(), pts);
471
- }
472
-
473
-
474
-
475
- void MTASVFGBuilder::handleStoreStoreNonSparse(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
476
- {
477
- PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
478
- pts &= pta->getPts(n2->getPAGDstNodeID());
479
-
480
- addTDEdges(n1->getId(), n2->getId(), pts);
481
- addTDEdges(n2->getId(), n1->getId(), pts);
482
- }
483
-
484
- void MTASVFGBuilder::handleStoreLoad(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
485
- {
486
- const SVFInstruction* i1 = n1->getInst();
487
- const SVFInstruction* i2 = n2->getInst();
488
- /// MHP
489
- if (ADDEDGE_NOMHP!=Options::AddModelFlag() && !mhp->mayHappenInParallel(i1, i2))
490
- return;
491
- /// Alias
492
- if (ADDEDGE_NOALIAS!=Options::AddModelFlag() && !pta->alias(n1->getPAGDstNodeID(), n2->getPAGSrcNodeID()))
493
- return;
494
-
495
-
496
- PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
497
- pts &= pta->getPts(n2->getPAGSrcNodeID());
498
-
499
- /// Lock
500
- /// todo: we only consider all cxtstmt of one instruction in one lock span,
501
- /// otherwise we think this instruction is not locked
502
- /// This constraint is too strong. All cxt lock under different cxt cannot be identified.
503
-
504
-
505
- if (ADDEDGE_NOLOCK!=Options::AddModelFlag() && lockana->isProtectedByCommonLock(i1, i2))
506
- {
507
- if (isTailofSpan(n1) && isHeadofSpan(n2))
508
- addTDEdges(n1->getId(), n2->getId(), pts);
509
- }
510
- else
511
- {
512
- addTDEdges(n1->getId(), n2->getId(), pts);
513
- }
514
- }
515
-
516
-
517
-
518
- void MTASVFGBuilder::handleStoreStore(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
519
- {
520
- const SVFInstruction* i1 = n1->getInst();
521
- const SVFInstruction* i2 = n2->getInst();
522
- /// MHP
523
- if (ADDEDGE_NOMHP!=Options::AddModelFlag() && !mhp->mayHappenInParallel(i1, i2))
524
- return;
525
- /// Alias
526
- if (ADDEDGE_NOALIAS!=Options::AddModelFlag() && !pta->alias(n1->getPAGDstNodeID(), n2->getPAGDstNodeID()))
527
- return;
528
-
529
- PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
530
- pts &= pta->getPts(n2->getPAGDstNodeID());
531
-
532
- /// Lock
533
- if (ADDEDGE_NOLOCK!=Options::AddModelFlag() && lockana->isProtectedByCommonLock(i1, i2))
534
- {
535
- if (isTailofSpan(n1) && isHeadofSpan(n2))
536
- addTDEdges(n1->getId(), n2->getId(), pts);
537
- if (isTailofSpan(n2) && isHeadofSpan(n1))
538
- addTDEdges(n2->getId(), n1->getId(), pts);
539
- }
540
- else
541
- {
542
- addTDEdges(n1->getId(), n2->getId(), pts);
543
- addTDEdges(n2->getId(), n1->getId(), pts);
544
- }
545
- }
546
-
547
- void MTASVFGBuilder::handleStoreLoadWithLockPrecisely(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
548
- {
549
- if (!pta->alias(n1->getPAGDstNodeID(), n2->getPAGSrcNodeID()))
550
- return;
551
-
552
- // PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
553
- // pts &= pta->getPts(n2->getPAGSrcNodeID());
554
- //
555
- // const SVFInstruction* i1 = n1->getInst();
556
- // const SVFInstruction* i2 = n2->getInst();
557
- //
558
- // NodeBS comlocks1;
559
- // NodeBS comlocks2;
560
- // lockana->getCommonLocks(i1, i2, comlocks1, comlocks2);
561
- //
562
- // outs()<<comlocks1.count() << " "<< comlocks2.count()<<"\n";
563
-
564
-
565
- // if(comlocks1.count() && comlocks2.count()) {
566
- // bool n1istail = false;
567
- // for (NodeBS::iterator it1 = comlocks1.begin(), ie1 = comlocks1.end(); it1 != ie1; ++it1) {
568
- // LockAnalysis::LockSpan lspan1 = lockana->getSpanfromCxtLock(*it1);
569
- // /// exist lock span, n1 is tail;
570
- // if (isTailofSpan(n1, lspan1))
571
- // n1istail = true;
572
- // }
573
- //
574
- // bool n2ishead = false;
575
- // for (NodeBS::iterator it2 = comlocks2.begin(), ie2 = comlocks2.end(); it2 != ie2; ++it2) {
576
- // LockAnalysis::LockSpan lspan2 = lockana->getSpanfromCxtLock(*it2);
577
- // /// exist lock span, n2 is head;
578
- // if (isHeadofSpan(n2, lspan2))
579
- // n2ishead = true;
580
- // }
581
- //
582
- // if (n1istail && n2ishead)
583
- // addTDEdges(n1->getId(), n2->getId(), pts);
584
- // } else {
585
- // addTDEdges(n1->getId(), n2->getId(), pts);
586
- // }
587
- }
588
- void MTASVFGBuilder::handleStoreStoreWithLockPrecisely(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta)
589
- {
590
- if (!pta->alias(n1->getPAGDstNodeID(), n2->getPAGDstNodeID()))
591
- return;
592
-
593
- // PointsTo pts = pta->getPts(n1->getPAGDstNodeID());
594
- // pts &= pta->getPts(n2->getPAGDstNodeID());
595
- //
596
- // const SVFInstruction* i1 = n1->getInst();
597
- // const SVFInstruction* i2 = n2->getInst();
598
-
599
-
600
- // NodeBS comlocks1;
601
- // NodeBS comlocks2;
602
- // lockana->getCommonLocks(i1, i2, comlocks1, comlocks2);
603
- // if(comlocks1.count() && comlocks2.count()) {
604
- // bool n1istail = false;
605
- // for (NodeBS::iterator it1 = comlocks1.begin(), ie1 = comlocks1.end(); it1 != ie1; ++it1) {
606
- // LockAnalysis::LockSpan lspan1 = lockana->getSpanfromCxtLock(*it1);
607
- // /// exist lock span, n1 is tail;
608
- // if (isTailofSpan(n1, lspan1))
609
- // n1istail = true;
610
- // }
611
- // bool n2ishead = false;
612
- // for (NodeBS::iterator it2 = comlocks2.begin(), ie2 = comlocks2.end(); it2 != ie2; ++it2) {
613
- // LockAnalysis::LockSpan lspan2 = lockana->getSpanfromCxtLock(*it2);
614
- // /// exist lock span, n2 is head;
615
- // if (isHeadofSpan(n2, lspan2))
616
- // n2ishead = true;
617
- // }
618
- // if (n1istail && n2ishead)
619
- // addTDEdges(n1->getId(), n2->getId(), pts);
620
- //
621
- //
622
- //
623
- // bool n2istail = false;
624
- // for (NodeBS::iterator it2 = comlocks2.begin(), ie2 = comlocks2.end(); it2 != ie2; ++it2) {
625
- // LockAnalysis::LockSpan lspan2 = lockana->getSpanfromCxtLock(*it2);
626
- // /// exist lock span, n2 is tail;
627
- // if (isTailofSpan(n2, lspan2))
628
- // n2istail = true;
629
- // }
630
- // bool n1ishead = false;
631
- // for (NodeBS::iterator it1 = comlocks1.begin(), ie1 = comlocks1.end(); it1 != ie1; ++it1) {
632
- // LockAnalysis::LockSpan lspan1 = lockana->getSpanfromCxtLock(*it1);
633
- // /// exist lock span, n1 is head;
634
- // if (isHeadofSpan(n1, lspan1))
635
- // n1ishead = true;
636
- // }
637
- // if (n2istail && n1ishead)
638
- // addTDEdges(n2->getId(), n1->getId(), pts);
639
- //
640
- // } else {
641
- // addTDEdges(n1->getId(), n2->getId(), pts);
642
- // addTDEdges(n2->getId(), n1->getId(), pts);
643
- // }
644
- }
645
-
646
-
647
- void MTASVFGBuilder::mergeSpan(NodeBS comlocks, InstSet& res)
648
- {
649
- // for (NodeBS::iterator it = comlocks.begin(), ie = comlocks.end(); it != ie; ++it) {
650
- // LockAnalysis::LockSpan lspan = lockana->getSpanfromCxtLock(*it);
651
- // for (LockAnalysis::LockSpan::const_iterator cts = lspan.begin(), ects = lspan.end(); cts!=ects; ++cts) {
652
- // res.insert((*cts).getStmt());
653
- // }
654
- // }
655
- }
656
-
657
- /// For o, n2-o->n1, n1 and n2 are write. Foreach n3:n1->n3, n2->n3; then remove n2->n1.
658
- void MTASVFGBuilder::readPrecision()
659
- {
660
-
661
- recordedges.clear();
662
- edge2pts.clear();
663
-
664
- for (SVFGNodeSet::iterator it1 = stnodeSet.begin(), eit1 = stnodeSet.end(); it1 != eit1; ++it1)
665
- {
666
- const StmtSVFGNode* n1 = SVFUtil::cast<StmtSVFGNode>(*it1);
667
-
668
- for (SVFGEdge::SVFGEdgeSetTy::iterator iter = n1->InEdgeBegin(); iter != n1->InEdgeEnd(); ++iter)
669
- {
670
- SVFGEdge* edge = *iter;
671
- if (edge->isIndirectVFGEdge() && SVFUtil::isa<StoreSVFGNode>(edge->getSrcNode()))
672
- {
673
- const StmtSVFGNode* n2 = SVFUtil::cast<StmtSVFGNode>(edge->getSrcNode());
674
-
675
- IndirectSVFGEdge* e = SVFUtil::cast<IndirectSVFGEdge>(edge);
676
- NodeBS pts = e->getPointsTo();
677
- PointsTo remove_pts;
678
-
679
- for (NodeBS::iterator o = pts.begin(), eo = pts.end(); o != eo; ++o)
680
- {
681
- SVFGNodeIDSet succ1 = getSuccNodes(n1, *o);
682
- SVFGNodeIDSet succ2 = getSuccNodes(n2, *o);
683
-
684
- bool remove = true;
685
- for (SVFGNodeIDSet::iterator sn1 = succ1.begin(), esn1 = succ1.end(); sn1 != esn1; sn1++)
686
- {
687
- if (!succ2.test(*sn1))
688
- {
689
- remove = false;
690
- break;
691
- }
692
- }
693
- if (remove)
694
- remove_pts.set(*o);
695
- }
696
-
697
- if (remove_pts.count())
698
- recordRemovingEdge(n2->getId(), n1->getId(), remove_pts);
699
- }
700
- }
701
- }
702
-
703
- performRemovingMHPEdges();
704
- }
705
-
706
- void MTASVFGBuilder::connectMHPEdges(PointerAnalysis* pta)
707
- {
708
- PCG* pcg = new PCG(pta);
709
- if ((ADDEDGE_NONSPARSE==Options::AddModelFlag()) && Options::UsePCG())
710
- {
711
- pcg->analyze();
712
- }
713
- collectLoadStoreSVFGNodes();
714
- recordedges.clear();
715
- edge2pts.clear();
716
-
717
- /// todo: we ignore rule 2 and 3. but so far I haven't added intra-thread value flow affected by fork
718
- /// and inter-thread value flow affected by join
719
- for (SVFGNodeSet::const_iterator it1 = stnodeSet.begin(), eit1 = stnodeSet.end(); it1!=eit1; ++it1)
720
- {
721
- const StmtSVFGNode* n1 = SVFUtil::cast<StmtSVFGNode>(*it1);
722
- const SVFInstruction* i1 = n1->getInst();
723
-
724
- for (SVFGNodeSet::const_iterator it2 = ldnodeSet.begin(), eit2 = ldnodeSet.end(); it2 != eit2; ++it2)
725
- {
726
- const StmtSVFGNode* n2 = SVFUtil::cast<StmtSVFGNode>(*it2);
727
- const SVFInstruction* i2 = n2->getInst();
728
- if (ADDEDGE_NONSPARSE==Options::AddModelFlag())
729
- {
730
- if (Options::UsePCG())
731
- {
732
- if (pcg->mayHappenInParallel(i1, i2) || mhp->mayHappenInParallel(i1, i2))
733
- handleStoreLoadNonSparse(n1, n2, pta);
734
- }
735
- else
736
- {
737
- handleStoreLoadNonSparse(n1, n2, pta);
738
- }
739
- }
740
- else
741
- {
742
- handleStoreLoad(n1, n2, pta);
743
- }
744
- }
745
-
746
- for (SVFGNodeSet::const_iterator it2 = std::next(it1), eit2 = stnodeSet.end(); it2!=eit2; ++it2)
747
- {
748
- const StmtSVFGNode* n2 = SVFUtil::cast<StmtSVFGNode>(*it2);
749
- const SVFInstruction* i2 = n2->getInst();
750
- if (ADDEDGE_NONSPARSE == Options::AddModelFlag())
751
- {
752
- if (Options::UsePCG())
753
- {
754
- if(pcg->mayHappenInParallel(i1, i2) || mhp->mayHappenInParallel(i1, i2))
755
- handleStoreStoreNonSparse(n1, n2, pta);
756
- }
757
- else
758
- {
759
- handleStoreStoreNonSparse(n1, n2, pta);
760
- }
761
- }
762
- else
763
- {
764
- handleStoreStore(n1, n2, pta);
765
- }
766
- }
767
- }
768
-
769
- if(Options::ReadPrecisionTDEdge() && ADDEDGE_NORP!=Options::AddModelFlag())
770
- {
771
- DBOUT(DGENERAL,outs()<<"Read precision edge removing \n");
772
- DBOUT(DMTA,outs()<<"Read precision edge removing \n");
773
- readPrecision();
774
- }
775
- }
776
-
777
- /*!
778
- * Initialize analysis
779
- */
780
- void FSMPTA::initialize(SVFModule* module)
781
- {
782
- PointerAnalysis::initialize();
783
-
784
- AndersenWaveDiff* ander = AndersenWaveDiff::createAndersenWaveDiff(getPAG());
785
- MTASVFGBuilder mtaSVFGBuilder(mhp,lockana);
786
- svfg = mtaSVFGBuilder.buildPTROnlySVFG(ander);
787
- setGraph(svfg);
788
- //AndersenWaveDiff::releaseAndersenWaveDiff();
789
-
790
- stat = new FlowSensitiveStat(this);
791
- }
792
-