ClearBlue
SEGSimpleSite.h
1 /*
2  * SEGSimpleSite.h
3  *
4  * Qingkai
5  *
6  * This node is to model simple sites.
7  */
8 
9 #ifndef IR_SEG_SEGSIMPLESITE_H
10 #define IR_SEG_SEGSIMPLESITE_H
11 
12 #include <llvm/IR/Instructions.h>
13 
14 #include "Analysis/Bitcode/BitcodeUtils.h"
15 #include "IR/SEG/SymbolicExprGraph.h"
16 #include "Persistence/PersistenceBasis.h"
17 
18 using namespace llvm;
19 
20 class PersistedSEGSimpleSite;
21 
22 class PersistedSEGDereferenceSite;
23 
24 class PersistedSEGSEGDivSite;
25 
26 class PersistedSEGCmpSite;
27 
28 class PersistedSEGGEPSite;
29 
30 class PersistedSEGAllocSite;
31 
32 class SEGSimpleSite : public SEGSiteBase {
33 public:
36  SEGSimpleSite(PersistedSEGSimpleSite *Site, SymbolicExprGraph *SEG)
37  : SEGSiteBase(Site, SEG) {}
38 
39  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
40  SEGSiteBase::assembleSEGObject(FuncSEGObjMap);
41  }
42 
43 protected:
44  SEGSimpleSite(SEGObjectKind K, Instruction *U, SymbolicExprGraph *G)
45  : SEGSiteBase(K, U, G) {}
46 
47 public:
48  static bool classof(const SEGObject *N) {
49  return N->getKind() >= SEGOBJK_SimpleSiteBegin &&
50  N->getKind() <= SEGOBJK_SimpleSiteEnd;
51  }
52 };
53 
55 public:
58  SEGDereferenceSite(PersistedSEGDereferenceSite *Site, SymbolicExprGraph *SEG)
59  : SEGSimpleSite(Site, SEG) {}
60 
61  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
62  SEGSimpleSite::assembleSEGObject(FuncSEGObjMap);
63  }
64 
65 private:
66  SEGNodeBase *PtrOp = nullptr;
67  SEGNodeBase *ValOp = nullptr;
68 
69  SEGDereferenceSite(Instruction *U, SymbolicExprGraph *G)
70  : SEGSimpleSite(SEGOBJK_DereferenceSite, U, G) {}
71 
72  friend class SymbolicExprGraph;
73  friend class SymbolicExprGraphBuilder;
74  friend class IntraFalcon;
75  friend class MantaIntraFalcon;
76 
77  void setPtrOperand(SEGNodeBase *Node) { PtrOp = Node; }
78 
79  void setValOperand(SEGNodeBase *Node) { ValOp = Node; }
80 
81 public:
82  virtual PersistedSEGObject *createPersistedObject() const {
83  PersistedSEGObject *PersistedObj = new PersistedSEGDereferenceSite;
84  return PersistedObj;
85  }
86 
87  bool deref(const SEGOperandNode *Node) const {
88  if (Value *Val = Node->getLLVMValue()) {
89  kvec<Value *> DerefPtrs;
90  getDerefPtrFromInst(*getInstruction(), DerefPtrs);
91  if (DerefPtrs.find(Val) != DerefPtrs.end()) {
92  return true;
93  }
94  }
95  return false;
96  }
97 
98  bool isLoad() { return isa<LoadInst>(getInstruction()); }
99 
100  bool isStore() { return isa<StoreInst>(getInstruction()); }
101 
102  SEGNodeBase *getPtrOperand() { return PtrOp; }
103 
104  SEGNodeBase *getValOperand() { return ValOp; }
105 
106  static bool classof(const SEGObject *N) {
107  return N->getKind() == SEGOBJK_DereferenceSite;
108  }
109 };
110 
111 class SEGCmpSite : public SEGSimpleSite {
112 public:
115  SEGCmpSite(PersistedSEGCmpSite *Site, SymbolicExprGraph *SEG)
116  : SEGSimpleSite(Site, SEG) {}
117 
118  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
119  SEGSimpleSite::assembleSEGObject(FuncSEGObjMap);
120  }
121 
122 private:
123  SEGCmpSite(Instruction *U, SymbolicExprGraph *G)
124  : SEGSimpleSite(SEGOBJK_CmpSite, U, G) {}
125 
126  ~SEGCmpSite() override = default;
127 
128  friend class SymbolicExprGraph;
129 
130 public:
131  virtual PersistedSEGObject *createPersistedObject() const {
132  PersistedSEGObject *PersistedObj = new PersistedSEGCmpSite;
133  return PersistedObj;
134  }
135 
136 public:
137  static bool classof(const SEGObject *N) {
138  return N->getKind() == SEGOBJK_CmpSite;
139  }
140 };
141 
142 class SEGDivSite : public SEGSimpleSite {
143 public:
146  SEGDivSite(PersistedSEGDivSite *Site, SymbolicExprGraph *SEG)
147  : SEGSimpleSite(Site, SEG) {}
148 
149  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
150  SEGSimpleSite::assembleSEGObject(FuncSEGObjMap);
151  }
152 
153 private:
154  SEGDivSite(Instruction *U, SymbolicExprGraph *G)
155  : SEGSimpleSite(SEGOBJK_DivSite, U, G) {}
156 
157  friend class SymbolicExprGraph;
158 
159 public:
160  virtual PersistedSEGObject *createPersistedObject() const {
161  PersistedSEGObject *PersistedObj = new PersistedSEGDivSite;
162  return PersistedObj;
163  }
164 
165 public:
166  static bool classof(const SEGObject *N) {
167  return N->getKind() == SEGOBJK_DivSite;
168  }
169 };
170 
171 class SEGGEPSite : public SEGSimpleSite {
172 public:
175  SEGGEPSite(PersistedSEGGEPSite *Site, SymbolicExprGraph *SEG)
176  : SEGSimpleSite(Site, SEG) {}
177 
178  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
179  SEGSimpleSite::assembleSEGObject(FuncSEGObjMap);
180  }
181 
182 private:
183  SEGNodeBase *PtrOp = nullptr;
184  std::vector<SEGNodeBase *> OffsetOps;
185 
186  SEGGEPSite(Instruction *U, SymbolicExprGraph *G)
187  : SEGSimpleSite(SEGOBJK_GEPSite, U, G) {}
188 
189  friend class SymbolicExprGraph;
190  friend class SymbolicExprGraphBuilder;
191 
192  void setPtrOperand(SEGNodeBase *N) { PtrOp = N; }
193 
194  void addOffsetOperand(SEGNodeBase *N) { OffsetOps.emplace_back(N); }
195 
196 public:
197  static bool classof(const SEGObject *N) {
198  return N->getKind() == SEGOBJK_GEPSite;
199  }
200 
201  Instruction *GEPUsedInstruction() const {
202  auto *SiteNode = getParentGraph()->findNode(getInstruction());
203  for (auto It = SiteNode->use_site_begin(); It != SiteNode->use_site_end();
204  It++) {
205  auto I = (*It)->getInstruction();
206  auto OpCode = I->getOpcode();
207  if (OpCode == Instruction::Load || OpCode == Instruction::Store) {
208  return I;
209  }
210  }
211  return nullptr;
212  }
213 
214  virtual PersistedSEGObject *createPersistedObject() const {
215  PersistedSEGObject *PersistedObj = new PersistedSEGGEPSite;
216  return PersistedObj;
217  }
218 
219  SEGNodeBase *getPtrOperand() { return PtrOp; }
220  std::vector<SEGNodeBase *> &getOffsetOperands() { return OffsetOps; }
221 };
222 
223 class SEGAllocSite : public SEGSimpleSite {
224 public:
227  SEGAllocSite(PersistedSEGAllocSite *Site, SymbolicExprGraph *SEG)
228  : SEGSimpleSite(Site, SEG) {}
229 
230  virtual void assembleSEGObject(std::map<int, SEGObject *> &FuncSEGObjMap) {
231  SEGSimpleSite::assembleSEGObject(FuncSEGObjMap);
232  }
233 
234 private:
235  SEGAllocSite(Instruction *U, SymbolicExprGraph *G)
236  : SEGSimpleSite(SEGOBJK_AllocSite, U, G) {}
237 
238  friend class SymbolicExprGraph;
239 
240 public:
241  virtual PersistedSEGObject *createPersistedObject() const {
242  PersistedSEGObject *PersistedObj = new PersistedSEGAllocSite;
243  return PersistedObj;
244  }
245 
246 public:
247  static bool classof(const SEGObject *N) {
248  return N->getKind() == SEGOBJK_AllocSite;
249  }
250 };
251 
252 #endif
SEGGEPSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:178
SEGGEPSite::SEGGEPSite
SEGGEPSite(PersistedSEGGEPSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:175
SEGSimpleSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:39
SEGOperandNode
Definition: SymbolicExprGraph.h:539
SymbolicExprGraph
Definition: SymbolicExprGraph.h:855
SEGDivSite::SEGDivSite
SEGDivSite(PersistedSEGDivSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:146
SEGSimpleSite::SEGSimpleSite
SEGSimpleSite(PersistedSEGSimpleSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:36
SEGCmpSite::SEGCmpSite
SEGCmpSite(PersistedSEGCmpSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:115
SEGCmpSite
Definition: SEGSimpleSite.h:111
SEGDereferenceSite
Definition: SEGSimpleSite.h:54
SEGDereferenceSite::SEGDereferenceSite
SEGDereferenceSite(PersistedSEGDereferenceSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:58
SymbolicExprGraphBuilder
Definition: SymbolicExprGraphBuilder.h:38
SEGAllocSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:230
SEGObject
Definition: SymbolicExprGraph.h:87
SEGCmpSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:118
SEGSimpleSite
Definition: SEGSimpleSite.h:32
SEGDivSite
Definition: SEGSimpleSite.h:142
SEGAllocSite::SEGAllocSite
SEGAllocSite(PersistedSEGAllocSite *Site, SymbolicExprGraph *SEG)
Definition: SEGSimpleSite.h:227
SEGSiteBase
Definition: SymbolicExprGraph.h:776
SEGDereferenceSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:61
SEGGEPSite
Definition: SEGSimpleSite.h:171
SEGSiteBase::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SymbolicExprGraph.cpp:674
SEGAllocSite
Definition: SEGSimpleSite.h:223
SEGNodeBase
The node base of symbolic expression graph.
Definition: SymbolicExprGraph.h:288
SEGDivSite::assembleSEGObject
virtual void assembleSEGObject(std::map< int, SEGObject * > &FuncSEGObjMap)
Assemble the SEG object's related objects.
Definition: SEGSimpleSite.h:149