Static Value-Flow Analysis
Loading...
Searching...
No Matches
MTA.h
Go to the documentation of this file.
1//===- MTA.h -- 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 * MTA.h
25 *
26 * Created on: May 14, 2014
27 * Author: Yulei Sui, Peng Di
28 *
29 * The base data race detector 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 MTA_H_
35#define MTA_H_
36
37#include <set>
38#include <string>
39#include <vector>
40#include <memory>
41#include <utility>
42#include "SVFIR/SVFIR.h"
43#include "SVFIR/SVFValue.h"
44#include "SVFIR/SVFStatements.h"
45#include "SVFIR/SVFVariables.h"
47#include "MTA/MHP.h"
48#include "MTA/LockAnalysis.h"
49#include "MTA/MTASVFGBuilder.h"
50#include "WPA/Andersen.h"
51#include "Graphs/CallGraph.h"
52
53namespace SVF
54{
55
56class PointerAnalysis;
57class AndersenWaveDiff;
58class AndersenBase;
59class ThreadCallGraph;
60class CallGraph;
61class MTAStat;
62class TCT;
63class MHP;
64class LockAnalysis;
65class SVFStmt;
66class SVFIR;
67class ICFGNode;
68// Forward declarations for the SlicedMTA slicing pipeline (see SlicedMTA impl).
69class SVFG;
70class FlowSensitive;
71class SlicedSVFGView;
72class MultiStageSlicer;
73class SingleSlicer;
74class SlicedSVFIRView;
75class SlicedTCT;
76
80class MTA
81{
82
83public:
85 MTA();
86
88 virtual ~MTA();
89
90
92 virtual bool runOnModule(SVFIR* module);
94 virtual MHP* computeMHP(TCT* tct);
98 virtual void reportRaces();
99
101 {
102 return mhp;
103 }
104
106 {
107 return lsa;
108 }
109
111 struct RacePair
112 {
115 RacePair(const SVFStmt* s1, const SVFStmt* s2)
116 : stmt1(statementLess(s2, s1) ? s2 : s1),
118 {
119 }
120
121 static bool statementLess(const SVFStmt* lhs, const SVFStmt* rhs)
122 {
123 return lhs->getEdgeID() < rhs->getEdgeID();
124 }
125
126 bool operator<(const RacePair& other) const
127 {
128 if (stmt1->getEdgeID() != other.stmt1->getEdgeID())
129 return stmt1->getEdgeID() < other.stmt1->getEdgeID();
130 return stmt2->getEdgeID() < other.stmt2->getEdgeID();
131 }
132 };
133
136 static std::set<const SVFStmt*> detectRace(
137 SVFIR* svfir, AndersenBase* pta, MHP* mhp, LockAnalysis* lockAnalysis,
138 CallGraph* callGraph, std::set<RacePair>& outRacePairs);
139
143
146 static bool hasThreadFunctions(CallGraph* callGraph);
147
148private:
159
161 {
163 bool locked;
165 std::vector<size_t> members;
166 };
167
169 {
173 bool locked;
175
176 bool operator<(const RaceClassKey& other) const;
177 };
178
180
182
183 static void collectRaceOccurrences(
184 SVFIR* svfir, AndersenBase* pta, MHP* mhp,
185 LockAnalysis* lockAnalysis, CallGraph* callGraph,
187 std::vector<RaceOccurrence>& occurrences,
189 static std::vector<RaceClass> buildRaceClasses(
190 const std::vector<RaceOccurrence>& occurrences,
191 const std::vector<size_t>& occurrenceIndices);
192 static void emitRacePairs(
193 MHP* mhp, LockAnalysis* lockAnalysis,
194 const std::vector<RaceOccurrence>& occurrences,
195 const std::vector<RaceClass>& classes,
196 std::set<RacePair>& outRacePairs);
197 static bool occurrencesRace(MHP* mhp, const RaceOccurrence& first, const RaceOccurrence& second);
198 static void commitRacePair(std::set<RacePair>& out,
201
203 std::unique_ptr<TCT> tct;
204 std::unique_ptr<MTAStat> stat;
207};
208
227{
228public:
231
232 // Out-of-line (defined where the member types are complete) so callers that
233 // only see the forward-declared unique_ptr member types need not be complete.
234 SlicedMTA();
236
237 SlicedMTA(const SlicedMTA&) = delete;
238 SlicedMTA& operator=(const SlicedMTA&) = delete;
239
241 bool runOnModule(SVFIR* pag, AndersenWaveDiff& preAnalysis);
242
243private:
244 // --- pipeline stages ---
245 bool runPreAnalysis();
250
252
254 static std::set<const ICFGNode*> collectICFGNodes(
255 SVFG* svfg, const NodeBS& svfgNodeIds);
256 static void reportPTASliceStatistics(
257 const std::set<const ICFGNode*>& icfgNodes);
259 {
262 };
263 static std::string raceStatementKey(const SVFStmt* statement);
264 static void updateDigest(u64_t& digest, const std::string& value);
265 static RaceDigests computeRaceDigests(const std::set<RacePair>& pairs);
267
271
274 BVDataPTAImpl* getMainPTA() const;
275
277 std::set<const SVFStmt*> getVulnerableStmts() const;
278
279 // --- race detection ---
281 std::set<RacePair> detectRacePairsOnSlicedGraph(
282 const std::set<RacePair>& preAnalysisRacePairs,
285
286 // --- pipeline state (owned unless noted) ---
287 SVFIR* svfir = nullptr;
288 // Main-phase context depth, set by runOnModule; the pre-analysis uses it
289 // to reconcile context-truncation-merged thread instances.
291 std::unique_ptr<TCT> tct;
292 std::unique_ptr<MHP> mhp;
293 std::unique_ptr<LockAnalysis> lockAnalysis;
294 // Inclusion-based Andersen's pre-analysis (a shared singleton, not owned
295 // here; the tool releases it after this object is destroyed). Feeds TCT/MHP/lock/
296 // race pre-analysis, the thread-aware VFG_pre, and the main FSMPTA.
299 std::unique_ptr<MTASVFGBuilder> preSVFGBuilder; // owns preSVFG
300 SVFG* preSVFG = nullptr;
301 std::unique_ptr<MultiStageSlicer> multiStageSlicer;
303 std::unique_ptr<SingleSlicer> singleSlicer;
308 // -mta-slicing-single: the one unified slice, computed in MTA slicing and reused
309 // (not recomputed) for PTA slicing so both stages share V_Single.
310 std::set<const ICFGNode*> singleSlicedNodes;
312 std::unique_ptr<SlicedSVFIRView> mtaSlicedView;
313 std::unique_ptr<SlicedSVFIRView> ptaSlicedView;
314 std::unique_ptr<SlicedSVFGView> slicedSVFGView;
315 std::unique_ptr<FlowSensitive> mainFSMPTA;
316 std::unique_ptr<SlicedTCT> slicedTCT;
317 std::unique_ptr<MHP> slicedMHP;
318 std::unique_ptr<LockAnalysis> slicedLockAnalysis;
319 bool hasThreadFunctions = false;
320 std::set<RacePair> racePairs;
321};
322
323} // End namespace SVF
324
325#endif /* MTA_H_ */
Definition MHP.h:52
std::vector< ThreadVFCandidate > ThreadVFCandidateList
Definition MTA.h:81
LockAnalysis * getLockAnalysis()
Definition MTA.h:105
static void emitRacePairs(MHP *mhp, LockAnalysis *lockAnalysis, const std::vector< RaceOccurrence > &occurrences, const std::vector< RaceClass > &classes, std::set< RacePair > &outRacePairs)
Definition MTA.cpp:318
virtual LockAnalysis * computeLocksets(TCT *tct)
Compute locksets.
Definition MTA.cpp:106
static std::vector< RaceClass > buildRaceClasses(const std::vector< RaceOccurrence > &occurrences, const std::vector< size_t > &occurrenceIndices)
Definition MTA.cpp:289
ThreadCallGraph * tcg
Definition MTA.h:202
std::unique_ptr< TCT > tct
Definition MTA.h:203
static bool occurrencesRace(MHP *mhp, const RaceOccurrence &first, const RaceOccurrence &second)
Definition MTA.cpp:197
virtual ~MTA()
Destructor.
Definition MTA.cpp:58
static PointsTo getGlobalObjectVariables(SVFIR *svfir)
Escape/points-to helpers for the shared detector.
Definition MTA.cpp:132
LockAnalysis * lsa
Definition MTA.h:206
Map< NodeID, std::vector< size_t > > ObjectToRaceOccurrences
Definition MTA.h:179
MHP * getMHP()
Definition MTA.h:100
MHP * mhp
Definition MTA.h:205
virtual MHP * computeMHP(TCT *tct)
Compute MHP.
Definition MTA.cpp:113
MTA()
Constructor.
Definition MTA.cpp:53
static void commitRacePair(std::set< RacePair > &out, const RaceOccurrence &first, const RaceOccurrence &second)
Definition MTA.cpp:207
static void collectRaceOccurrences(SVFIR *svfir, AndersenBase *pta, MHP *mhp, LockAnalysis *lockAnalysis, CallGraph *callGraph, const PointsTo &escapedObjects, std::vector< RaceOccurrence > &occurrences, ObjectToRaceOccurrences &objectToOccurrences)
Helpers for the equivalence-class race detector.
Definition MTA.cpp:229
std::unique_ptr< MTAStat > stat
Definition MTA.h:204
static std::set< const SVFStmt * > detectRace(SVFIR *svfir, AndersenBase *pta, MHP *mhp, LockAnalysis *lockAnalysis, CallGraph *callGraph, std::set< RacePair > &outRacePairs)
Definition MTA.cpp:372
static bool hasThreadFunctions(CallGraph *callGraph)
Definition MTA.cpp:1326
virtual bool runOnModule(SVFIR *module)
We start the pass here.
Definition MTA.cpp:67
virtual void reportRaces()
Run the shared detector and print a race report.
Definition MTA.cpp:420
static PointsTo getPointsToClosure(AndersenBase *pta, const PointsTo &pts)
Definition MTA.cpp:161
EdgeID getEdgeID() const
Return Edge ID.
NodeBS preCandidateSolveNodeIds
Definition MTA.h:302
bool runWholeProgramDetection()
Definition MTA.cpp:1177
static RaceDigests computeRaceDigests(const std::set< RacePair > &pairs)
Definition MTA.cpp:543
std::unique_ptr< LockAnalysis > lockAnalysis
Definition MTA.h:293
SVFIR * svfir
Definition MTA.h:287
std::set< RacePair > racePairs
Definition MTA.h:320
AndersenWaveDiff * preAndersen
Definition MTA.h:297
bool runPreAnalysis()
Definition MTA.cpp:631
bool hasThreadFunctions
Definition MTA.h:319
std::unique_ptr< MTASVFGBuilder > preSVFGBuilder
Definition MTA.h:299
static std::string raceStatementKey(const SVFStmt *statement)
Definition MTA.cpp:504
bool runPTASlicingAndAnalysis()
Definition MTA.cpp:904
MTASVFGBuilder::ThreadVFCandidateList selectedThreadVFCandidates
Definition MTA.h:307
bool runFinalRaceDetection()
Definition MTA.cpp:1087
std::set< const SVFStmt * > getVulnerableStmts() const
Union of both statements of every candidate race pair (the slice targets).
Definition MTA.cpp:594
ThreadCallGraph * threadCallGraph
Definition MTA.h:298
std::unique_ptr< LockAnalysis > slicedLockAnalysis
Definition MTA.h:318
std::unique_ptr< SlicedSVFIRView > mtaSlicedView
Definition MTA.h:312
std::set< RacePair > detectRacePairsOnSlicedGraph(const std::set< RacePair > &preAnalysisRacePairs, BVDataPTAImpl *slicedPTA, MHP *slicedMHP, LockAnalysis *slicedLockAnalysis)
Refine the pre-analysis candidate pairs with main-phase ILA and FSAM.
Definition MTA.cpp:1345
bool runMTASlicingAndAnalysis()
Definition MTA.cpp:738
std::unique_ptr< MultiStageSlicer > multiStageSlicer
Definition MTA.h:301
static std::set< const ICFGNode * > collectICFGNodes(SVFG *svfg, const NodeBS &svfgNodeIds)
Definition MTA.cpp:470
SlicedMTA(const SlicedMTA &)=delete
static void updateDigest(u64_t &digest, const std::string &value)
Definition MTA.cpp:532
std::set< const ICFGNode * > singleSlicedNodes
Definition MTA.h:310
std::unique_ptr< SingleSlicer > singleSlicer
Definition MTA.h:303
void buildPreAnalysisSVFG()
Definition MTA.cpp:608
static void reportPTASliceStatistics(const std::set< const ICFGNode * > &icfgNodes)
Definition MTA.cpp:486
NodeBS singleSlicedSVFGNodeIds
Definition MTA.h:311
u32_t mainContextDepth
Definition MTA.h:290
SlicedMTA & operator=(const SlicedMTA &)=delete
bool runOnModule(SVFIR *pag, AndersenWaveDiff &preAnalysis)
Run the slicing pipeline with its prepared Andersen pre-analysis.
Definition MTA.cpp:1276
std::unique_ptr< SlicedSVFIRView > ptaSlicedView
Definition MTA.h:313
std::unique_ptr< TCT > tct
Definition MTA.h:291
std::unique_ptr< SlicedSVFGView > slicedSVFGView
Definition MTA.h:314
std::unique_ptr< SlicedTCT > slicedTCT
Definition MTA.h:316
std::unique_ptr< MHP > mhp
Definition MTA.h:292
std::unique_ptr< MHP > slicedMHP
Definition MTA.h:317
static void reportOriginalStatistics(SVFIR *svfir)
Pipeline utilities shared by the sliced and whole-program paths.
Definition MTA.cpp:445
BVDataPTAImpl * getMainPTA() const
Definition MTA.cpp:587
std::unique_ptr< FlowSensitive > mainFSMPTA
Definition MTA.h:315
SVFG * preSVFG
Definition MTA.h:300
for isBitcode
Definition BasicTypes.h:70
unsigned long long u64_t
Definition GeneralType.h:69
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
bool operator<(const RaceClassKey &other) const
Definition MTA.cpp:214
const NodeBS * interleaving
Definition MTA.h:172
std::vector< size_t > members
Definition MTA.h:165
size_t representative
Definition MTA.h:164
One occurrence of a memory access under one thread instance.
Definition MTA.h:151
const NodeBS * interleaving
Definition MTA.h:156
const SVFStmt * stmt
Definition MTA.h:152
const ICFGNode * node
Definition MTA.h:153
A race pair: two statements that may race.
Definition MTA.h:112
const SVFStmt * stmt2
Definition MTA.h:114
bool operator<(const RacePair &other) const
Definition MTA.h:126
RacePair(const SVFStmt *s1, const SVFStmt *s2)
Definition MTA.h:115
static bool statementLess(const SVFStmt *lhs, const SVFStmt *rhs)
Definition MTA.h:121
const SVFStmt * stmt1
Definition MTA.h:113