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