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