Static Value-Flow Analysis
MTAResultValidator.h
Go to the documentation of this file.
1 /*
2  * MTAResultValidator.h
3  *
4  * Created on: 29/06/2015
5  * Author: Peng Di and Ding Ye
6  */
7 
8 #ifndef MTARESULTVALIDATOR_H_
9 #define MTARESULTVALIDATOR_H_
10 
11 #include "MTA/MHP.h"
12 #include "SVF-LLVM/LLVMUtil.h"
13 
18 namespace SVF
19 {
20 typedef unsigned NodeID;
21 
23 {
24 
25 public:
26  typedef int INTERLEV_FLAG;
28  mhp(mh)
29  {
31  tdAPI = tcg->getThreadAPI();
32  mod = mhp->getTCT()->getSVFModule();
33  }
34  // Destructor
36  {
37  }
38 
39  // Analysis
40  void analyze();
41  inline SVFModule* getModule() const
42  {
43  return mod;
44  }
45 protected:
46 
47  /*
48  * Assistant functions
49  */
50 
51  // Split string
52  std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
53  std::vector<std::string> split(const std::string &s, char delim);
54 
55  // Get special arguments of given call sites
56  NodeID getIntArg(const Instruction* inst, unsigned int arg_num);
57  std::vector<std::string> getStringArg(const Instruction* inst, unsigned int arg_num);
58  CallStrCxt getCxtArg(const Instruction* inst, unsigned int arg_num);
59 
60  /*
61  * Get the previous LoadInst or StoreInst from Instruction "I" in the
62  * same BasicBlock. Return nullptr if none exists.
63  */
65 
66  // Compare two cxts
67  bool matchCxt(const CallStrCxt cxt1, const CallStrCxt cxt2) const;
68 
69  // Dump calling context information
70  void dumpCxt(const CallStrCxt& cxt) const;
71 
72  void dumpInterlev(NodeBS& lev);
73 
74  // Get the validation result string of a single validation scenario.
75  inline std::string getOutput(const char *scenario, bool analysisRes);
76  inline std::string getOutputforInterlevAnalysis(const char *scenario, INTERLEV_FLAG analysisRes);
77 
78  /*
79  * Collect the callsite targets for validations.
80  * The targets are labeled by "cs1:", "cs2:"... that are the names of its basic blocks.
81  * The collected targets are stored in csnumToInstMap that maps label "cs1" to its Callsite.
82  */
84 
85  /*
86  * Collect the CxtThread targets for validations.
87  * The collected targets are stored in vthdToCxt that maps vthd to cxt.
88  */
90 
91  /*
92  * Collect TCT targets for validations.
93  * The collected targets are stored in rthdToChildren.
94  */
95  bool collectTCTTargets();
96 
97  /*
98  * Collect the thread interleaving targets for validations.
99  * The collected targets are stored in instToTSMap and threadStmtToInterLeaving.
100  */
102 
103  /*
104  * Perform validation for Cxtthread.
105  * If correct, the validator maps given thread vthd to static CxtThread rthd stored in vthdTorthd.
106  */
107  bool validateCxtThread();
108 
109  /*
110  * Perform validation for TCT.
111  */
112  bool validateTCT();
113 
114  /*
115  * Perform validation for thread interleaving.
116  */
118 
119 private:
120 
125 
127 
128  MHP::InstToThreadStmtSetMap instToTSMap; // Map a instruction to CxtThreadStmtSet
130 
131  static constexpr char const *CXT_THREAD = "CXT_THREAD";
132  static constexpr char const *INTERLEV_ACCESS = "INTERLEV_ACCESS";
133  static constexpr char const *TCT_ACCESS = "TCT_ACCESS";
134 
145 
146  static const INTERLEV_FLAG INTERLEV_TRUE = 0x01;
147  static const INTERLEV_FLAG INTERLEV_IMPRECISE = 0x02;
148  static const INTERLEV_FLAG INTERLEV_UNSOUND = 0x04;
150 };
151 
152 
153 
170 {
171 public:
172  typedef int RC_FLAG;
173 
178  {
179  public:
182  const RC_FLAG flags) :
183  I1(I1), I2(I2), flags(flags)
184  {
185  }
186 
188 
189  inline bool isFlaged(const RC_FLAG flag) const
190  {
191  return flags & flag;
192  }
193  inline const Instruction* getInstruction1() const
194  {
195  return I1;
196  }
197  inline const Instruction* getInstruction2() const
198  {
199  return I2;
200  }
202 
203  private:
204  const Instruction* I1;
205  const Instruction* I2;
207  };
208 
211  {
212  release();
213  }
214 
216  void init(SVFModule* M)
217  {
218  this->M = M;
221  }
222 
224  void analyze()
225  {
226  validateAll();
227  }
228 
230  void release()
231  {
232  }
233 
235  inline bool hasValidationTarget() const
236  {
237  return !accessPairs.empty();
238  }
239 
240 protected:
243 
244  virtual bool mayAccessAliases(const Instruction* I1,
245  const Instruction* I2)
246  {
248  return true;
249  }
250  virtual bool mayHappenInParallel(const Instruction* I1,
251  const Instruction* I2)
252  {
254  return true;
255  }
256  virtual bool protectedByCommonLocks(const Instruction* I1,
257  const Instruction* I2)
258  {
260  return true;
261  }
262  virtual bool mayHaveDataRace(const Instruction* I1,
263  const Instruction* I2)
264  {
266  return true;
267  }
269 
276 
278  void validateAll();
279 
281  inline std::string getOutput(const char *scenario,
282  bool analysisRes, bool expectedRes)
283  {
284  std::string ret(scenario);
285  ret += "\t";
286  if (expectedRes)
287  ret += " T: ";
288  else
289  ret += " F: ";
290  if (analysisRes == expectedRes)
291  ret += SVFUtil::sucMsg("SUCCESS");
292  else
293  ret += SVFUtil::errMsg("FAILURE");
294  return ret;
295  }
296 
297 private:
299  std::vector<AccessPair> accessPairs;
301 
306  static bool compare(const CallBase* CI1, const CallBase* CI2)
307  {
308  const Value *V1 = CI1->getArgOperand(0);
309  const Value *V2 = CI2->getArgOperand(0);
310  const ConstantInt* C1 = SVFUtil::dyn_cast<ConstantInt>(V1);
311  const ConstantInt* C2 = SVFUtil::dyn_cast<ConstantInt>(V2);
312  assert(0 != C1 && 0 != C2);
313  return C1->getZExtValue() < C2->getZExtValue();
314  }
315 
322  const Instruction* I);
323 
325 
326  static const RC_FLAG RC_MHP = 0x01;
327  static const RC_FLAG RC_ALIASES = 0x02;
328  static const RC_FLAG RC_PROTECTED = 0x04;
329  static const RC_FLAG RC_RACE = 0x10;
331 
334  static constexpr char const *RC_ACCESS = "RC_ACCESS";
335 };
336 } // namespace SVF end
337 #endif /* MTARESULTVALIDATOR_H_ */
const char *const string
Definition: cJSON.h:172
Definition: MHP.h:46
TCT * getTCT() const
Get Thread Creation Tree.
Definition: MHP.h:82
ThreadCallGraph * getThreadCallGraph() const
Get ThreadCallGraph.
Definition: MHP.h:76
Map< const SVFInstruction *, CxtThreadStmtSet > InstToThreadStmtSetMap
Definition: MHP.h:55
Map< CxtThreadStmt, NodeBS > ThreadStmtToThreadInterleav
Definition: MHP.h:54
static const INTERLEV_FLAG INTERLEV_UNSOUND
static constexpr char const * INTERLEV_ACCESS
Map< NodeID, CallStrCxt > vthdToCxtMap
std::vector< std::string > getStringArg(const Instruction *inst, unsigned int arg_num)
void dumpInterlev(NodeBS &lev)
INTERLEV_FLAG validateInterleaving()
Map< NodeID, const SVFInstruction * > csnumToInst
rthdToChildrenMap rthdToChildren
static const INTERLEV_FLAG INTERLEV_TRUE
Constant INTERLEV_FLAG values.
bool matchCxt(const CallStrCxt cxt1, const CallStrCxt cxt2) const
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Map< NodeID, Set< NodeID > > rthdToChildrenMap
Map< NodeID, NodeID > vthdTorthdMap
static const INTERLEV_FLAG INTERLEV_IMPRECISE
MHP::InstToThreadStmtSetMap instToTSMap
std::string getOutput(const char *scenario, bool analysisRes)
static constexpr char const * CXT_THREAD
Map a statement to its thread interleavings.
Map< NodeID, NodeID > rthdTovthdMap
const Instruction * getPreviousMemoryAccessInst(const Instruction *I)
std::string getOutputforInterlevAnalysis(const char *scenario, INTERLEV_FLAG analysisRes)
MHP::ThreadStmtToThreadInterleav threadStmtToInterLeaving
NodeID getIntArg(const Instruction *inst, unsigned int arg_num)
static constexpr char const * TCT_ACCESS
void dumpCxt(const CallStrCxt &cxt) const
SVFModule * getModule() const
CallStrCxt getCxtArg(const Instruction *inst, unsigned int arg_num)
bool isFlaged(const RC_FLAG flag) const
Class member access.
AccessPair(const Instruction *I1, const Instruction *I2, const RC_FLAG flags)
Constructor.
const Instruction * getInstruction1() const
const Instruction * getInstruction2() const
Validate the result of concurrent analysis.
static const RC_FLAG RC_ALIASES
virtual bool mayHaveDataRace(const Instruction *I1, const Instruction *I2)
static bool compare(const CallBase *CI1, const CallBase *CI2)
void validateAll()
Perform validation for all targets.
void release()
Release resource.
bool hasValidationTarget() const
Check if the input program has validation target.
const Instruction * getPreviousMemoryAccessInst(const Instruction *I)
virtual ~RaceResultValidator()
Destructor.
std::vector< AccessPair > accessPairs
static const RC_FLAG RC_MHP
Constant RC_FLAG values.
static const RC_FLAG RC_RACE
static const RC_FLAG RC_PROTECTED
static constexpr char const * RC_ACCESS
virtual bool mayAccessAliases(const Instruction *I1, const Instruction *I2)
virtual bool protectedByCommonLocks(const Instruction *I1, const Instruction *I2)
void init(SVFModule *M)
Initialization.
std::string getOutput(const char *scenario, bool analysisRes, bool expectedRes)
Get the validation result string of a single validation scenario.
virtual bool mayHappenInParallel(const Instruction *I1, const Instruction *I2)
SVFModule * getSVFModule() const
Get SVFFModule.
Definition: TCT.h:176
ThreadAPI * getThreadAPI() const
Thread API.
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition: SVFUtil.cpp:53
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition: SVFUtil.cpp:76
for isBitcode
Definition: BasicTypes.h:68
llvm::CallBase CallBase
Definition: BasicTypes.h:146
unsigned NodeID
llvm::Instruction Instruction
Definition: BasicTypes.h:87
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82
std::vector< u32_t > CallStrCxt
Definition: GeneralType.h:122
llvm::ConstantInt ConstantInt
Definition: BasicTypes.h:125