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