ClearBlue
SEGTrace.h
1 /*
2  * SEGTrace.h
3  *
4  * Created on: 2016.11.4
5  * Author: andyzhou
6  */
7 
8 #ifndef INCLUDE_IR_SEG_SEGTRACE_H_
9 #define INCLUDE_IR_SEG_SEGTRACE_H_
10 
11 #include "rapidjson/document.h"
12 
13 #include "IR/LLVMValueTrace.h"
14 #include "IR/SEG/SEGMapBase.h"
15 #include "IR/Trace.h"
16 
17 using namespace llvm;
18 
19 class SEGRegionNode;
20 
21 class SEGTrace : public Trace<const SEGObject *> {
22 public:
23  typedef uint64_t StepType;
24  const static StepType STY_VALUE = 0x1;
25  const static StepType STY_USE_SITE = 0x2;
26 
27  const StepType DEFAULT_STEP_TYPE = STY_VALUE | STY_USE_SITE;
28 
29 private:
30  kvec<StepType> STys;
31 
32  // Additional conditions such as for NPD trace, we have ptr==null, and some
33  // path choices are also modelled here
34  kvec<SEGRegionNode *> AdditionalConds;
35 
36 protected:
37  // Guess the source node index of the last effect value flow of the bug trace
38  // if exist The sink of the last effect value flow is usually the key index
39  // This can be used to override "getLEVFSrcIdx" function
40  //
41  // In Basic IR, a value can be accessed from the value cached in register or
42  // by a load instruction accessing the main memory By setting isConsiderLoad =
43  // true, we consider load instruction as a source of the last effective value
44  // flow, or otherwise, we go past the load instruction and directly find the
45  // store instruction
46  int guessLEVFSrcIdx(bool IsConsiderLoad) const;
47 
48 public:
49  SEGTrace();
50 
51  SEGTrace(const SEGTrace &T);
52 
53  SEGTrace(const kvec<const SEGObject *> &Trace);
54 
56  SEGTrace(const SEGObject *Obj);
57 
58  SEGTrace(const SEGObject *Obj1, const SEGObject *Obj2);
59 
60  // Construct SEG trace from Value Trace
61  SEGTrace(const LLVMValueTrace *ValueTrace, SEGMapBase *SEGs);
62 
63  SEGTrace &operator=(const SEGTrace &T);
64 
65  // Reset the trace with LLVM Value Trace
66  virtual void resetWithLLVMValueTrace(const LLVMValueTrace *ValueTrace,
67  SEGMapBase *SEGs);
68 
69  virtual ~SEGTrace();
70 
71  // Get the real start idx of the trace that matters
72  // For some bug types, the first several steps shall be some preparation nodes
73  // Usually, returns 0, needing override when a class of trace has special
74  // needs that the first few steps are not considered
75  virtual int getValidStartIdx() const;
76 
77  // Get the key step index in the trace where vulnerability occurs
78  // SOURCE_SINK/SIMPLE_VALUE_FLOW/NON_VALUE_FLOW : return the last step
79  // SOURCE_NO_SINK : return the first step indicating the source
80  virtual int getKeyIdx() const;
81 
82  // Get the source node index of the last effect value flow of the bug trace if
83  // exist The sink of the last effect value flow is usually the key index
84  //
85  // By default, this function returns as follows :
86  // SOURCE_SINK/SIMPLE_VALUE_FLOW :
87  // guessLEVFSrcIdx(isConsiderLoad), which tries to guess the last value
88  // flow to the key step returned by getKeyIdx()
89  // SOURCE_NO_SINK/NON_VALUE_FLOW
90  // the last step of the trace, meaning that a bug report does not have
91  // such last effect value flow
92  //
93  // Please extend SEGTrace by overriding this function to support special last
94  // effect value flow source computation for e.g. domination checking
95  // guessLEVFSrcIdx() can be used to guess the last effect value flow index for
96  // common traces like NPD Trace, which can be used for the overriding of this
97  // function
98  //
99  // In Basic IR, a value can be accessed from the value cached in register or
100  // by a load instruction accessing the main memory By setting isConsiderLoad =
101  // true, we consider load instruction as a source of the last effective value
102  // flow, or otherwise, we go past the load instruction and directly find the
103  // store instruction
104  virtual int getLEVFSrcIdx(bool isConsiderLoad = false) const;
105 
106  // get the trace step type for the given StepIdx
107  StepType getStepType(int StepIdx) const;
108 
109  // get the trace step type for the given StepIdx to STy
110  void setStepType(int StepIdx, StepType STy);
111 
112  // Get the number of additional conditions for the trace
113  int getNumAdditionalConds() const;
114 
115  // Get the additional condition with index idx ranges in [0,
116  // getNumAdditionalConds)
117  SEGRegionNode *getAdditionalCond(int idx) const;
118 
119  // Add an additional condition modelled using SEG region node
120  void addAdditionalCond(SEGRegionNode *cond);
121 
122  // Clear all the additional conditions
123  void clearAdditionalCond();
124 
128  template <typename Predicate>
129  std::pair<const SEGObject *, int> find(int StartIndex, Predicate P) const {
130  for (int I = StartIndex, E = get_length(); I < E; ++I) {
131  auto *O = at(I);
132  if (P(O)) {
133  return std::move(std::make_pair(O, I));
134  }
135  }
136 
137  return std::move(std::make_pair(nullptr, get_length()));
138  }
139 
140  virtual void print(raw_ostream &O) override;
141 
142  virtual rapidjson::Value
143  makeJSON(rapidjson::Document::AllocatorType &allocator) const;
144 };
145 
146 #endif /* INCLUDE_IR_SEG_SEGTRACE_H_ */
SEGMapBase
Definition: SEGMapBase.h:19
SEGObject
Definition: SymbolicExprGraph.h:87
SEGRegionNode
Definition: SEGRegionNode.h:36
SEGTrace::find
std::pair< const SEGObject *, int > find(int StartIndex, Predicate P) const
Definition: SEGTrace.h:129
SEGTrace
Definition: SEGTrace.h:21