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 
17 using namespace llvm;
18 
19 class SEGSimpleSite : public SEGSiteBase {
20 protected:
21  SEGSimpleSite(SEGObjectKind K, Instruction *U, SymbolicExprGraph *G,
22  bool fromDisk)
23  : SEGSiteBase(K, U, G, fromDisk) {}
24 
25 public:
26  static bool classof(const SEGObject *N) {
27  return N->getKind() >= SEGOBJK_SimpleSiteBegin &&
28  N->getKind() <= SEGOBJK_SimpleSiteEnd;
29  }
30 };
31 
33 private:
34  SEGNodeBase *PtrOp = nullptr;
35  SEGNodeBase *ValOp = nullptr;
36 
37  SEGDereferenceSite(Instruction *U, SymbolicExprGraph *G, bool fromDisk)
38  : SEGSimpleSite(SEGOBJK_DereferenceSite, U, G, fromDisk) {}
39 
40  friend class SymbolicExprGraph;
41  friend class SymbolicExprGraphBuilder;
42  friend class IntraFalcon;
43  friend class MantaIntraFalcon;
44  friend class SEGSerializer;
45 
46  void setPtrOperand(SEGNodeBase *Node) { PtrOp = Node; }
47 
48  void setValOperand(SEGNodeBase *Node) { ValOp = Node; }
49 
50 public:
51  bool deref(const SEGOperandNode *Node) const {
52  if (Value *Val = Node->getLLVMValue()) {
53  kvec<Value *> DerefPtrs;
54  getDerefPtrFromInst(*getInstruction(), DerefPtrs);
55  if (DerefPtrs.find(Val) != DerefPtrs.end()) {
56  return true;
57  }
58  }
59  return false;
60  }
61 
62  bool isLoad() { return isa<LoadInst>(getInstruction()); }
63 
64  bool isStore() { return isa<StoreInst>(getInstruction()); }
65 
66  SEGNodeBase *getPtrOperand() const { return PtrOp; }
67 
68  SEGNodeBase *getValOperand() const { return ValOp; }
69 
70  static bool classof(const SEGObject *N) {
71  return N->getKind() == SEGOBJK_DereferenceSite;
72  }
73 };
74 
75 class SEGCmpSite : public SEGSimpleSite {
76 private:
77  SEGCmpSite(Instruction *U, SymbolicExprGraph *G, bool fromDisk)
78  : SEGSimpleSite(SEGOBJK_CmpSite, U, G, fromDisk) {}
79 
80  ~SEGCmpSite() override = default;
81 
82  friend class SymbolicExprGraph;
83 
84 public:
85  static bool classof(const SEGObject *N) {
86  return N->getKind() == SEGOBJK_CmpSite;
87  }
88 };
89 
90 class SEGDivSite : public SEGSimpleSite {
91 private:
92  SEGDivSite(Instruction *U, SymbolicExprGraph *G, bool fromDisk)
93  : SEGSimpleSite(SEGOBJK_DivSite, U, G, fromDisk) {}
94 
95  friend class SymbolicExprGraph;
96 
97 public:
98  static bool classof(const SEGObject *N) {
99  return N->getKind() == SEGOBJK_DivSite;
100  }
101 };
102 
103 class SEGGEPSite : public SEGSimpleSite {
104 private:
105  SEGNodeBase *PtrOp = nullptr;
106  std::vector<SEGNodeBase *> OffsetOps;
107 
108  SEGGEPSite(Instruction *U, SymbolicExprGraph *G, bool fromDisk)
109  : SEGSimpleSite(SEGOBJK_GEPSite, U, G, fromDisk) {}
110 
111  friend class SymbolicExprGraph;
112  friend class SymbolicExprGraphBuilder;
113  friend class SEGSerializer;
114 
115  void setPtrOperand(SEGNodeBase *N) { PtrOp = N; }
116 
117  void addOffsetOperand(SEGNodeBase *N) { OffsetOps.emplace_back(N); }
118 
119 public:
120  static bool classof(const SEGObject *N) {
121  return N->getKind() == SEGOBJK_GEPSite;
122  }
123 
124  Instruction *GEPUsedInstruction() const {
125  auto *SiteNode = getParentGraph()->findNode(getInstruction());
126  for (auto It = SiteNode->use_site_begin(); It != SiteNode->use_site_end();
127  It++) {
128  auto I = (*It)->getInstruction();
129  auto OpCode = I->getOpcode();
130  if (OpCode == Instruction::Load || OpCode == Instruction::Store) {
131  return I;
132  }
133  }
134  return nullptr;
135  }
136 
137  SEGNodeBase *getPtrOperand() const { return PtrOp; }
138  std::vector<SEGNodeBase *> &getOffsetOperands() { return OffsetOps; }
139 };
140 
141 class SEGAllocSite : public SEGSimpleSite {
142 private:
143  SEGAllocSite(Instruction *U, SymbolicExprGraph *G, bool fromDisk)
144  : SEGSimpleSite(SEGOBJK_AllocSite, U, G, fromDisk) {}
145 
146  friend class SymbolicExprGraph;
147 
148 public:
149  static bool classof(const SEGObject *N) {
150  return N->getKind() == SEGOBJK_AllocSite;
151  }
152 };
153 
154 #endif
SEGOperandNode
Definition: SymbolicExprGraph.h:456
SymbolicExprGraph
Definition: SymbolicExprGraph.h:708
SEGCmpSite
Definition: SEGSimpleSite.h:75
SEGDereferenceSite
Definition: SEGSimpleSite.h:32
SymbolicExprGraphBuilder
Definition: SymbolicExprGraphBuilder.h:40
SEGObject
Definition: SymbolicExprGraph.h:76
SEGSimpleSite
Definition: SEGSimpleSite.h:19
SEGDivSite
Definition: SEGSimpleSite.h:90
SEGSiteBase
Definition: SymbolicExprGraph.h:663
SEGGEPSite
Definition: SEGSimpleSite.h:103
SEGAllocSite
Definition: SEGSimpleSite.h:141
SEGNodeBase
The node base of symbolic expression graph.
Definition: SymbolicExprGraph.h:244