ROSE  0.11.145.0
virtualBinCFG.h
1 #ifndef VIRTUAL_BIN_CFG_H
2 #define VIRTUAL_BIN_CFG_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <stdint.h>
7 #include "rosedll.h"
8 
9 //class AttachedPreprocessingInfoType;
10 class SgAsmInstruction;
11 class SgAsmStatement;
12 
13 namespace VirtualBinCFG {
14 
15  class CFGEdge;
16 
17  enum EdgeConditionKind
18  {
19  eckUnconditional, // Normal, unconditional edge
20  eckTrue, // True case of a two-way branch
21  eckFalse, // False case of a two-way branch
22  eckCaseLabel, // Case label (constant is given by caseLabel())
23  eckDefault // Default label
24  };
25 
26  typedef std::set<rose_addr_t> AddressSet;
27  typedef std::map<rose_addr_t, SgAsmInstruction*> AddressToInstructionMap;
28  typedef std::map<SgAsmInstruction*, AddressSet> InstructionToAddressesMap;
29  typedef std::map<SgAsmStatement*, AddressSet> StatementToAddressesMap;
30 
31  struct ROSE_DLL_API AuxiliaryInformation {
32  AddressToInstructionMap addressToInstructionMap;
33  InstructionToAddressesMap indirectJumpTargets;
34  StatementToAddressesMap returnTargets; /* statement is SgAsmBlock or SgAsmFunction */
35  InstructionToAddressesMap incomingEdges;
36 
37  public:
38 
41 
43  SgAsmInstruction *getInstructionAtAddress(rose_addr_t addr) const {
44  AddressToInstructionMap::const_iterator i = addressToInstructionMap.find(addr);
45  if (i == addressToInstructionMap.end()) return NULL;
46  return i->second;
47  }
48 
49  /* NOTE: this is not the transpose of getPossiblePredecessors()! */
50  const AddressSet& getPossibleSuccessors(SgAsmInstruction* insn) const;
51 
52  const AddressSet& getPossiblePredecessors(SgAsmInstruction* insn) const {
53  static const AddressSet emptySet;
54  InstructionToAddressesMap::const_iterator predsIter = incomingEdges.find(insn);
55  if (predsIter == incomingEdges.end()) {
56  return emptySet;
57  } else {
58  return predsIter->second;
59  }
60  }
61  };
62 
63  class CFGNode {
64  SgAsmInstruction *node;
65 #if 0 // [Robb Matzke 2021-03-17]: unused
66  const AuxiliaryInformation *info;
67 #endif
68  public:
69  explicit CFGNode(SgAsmInstruction *node, const AuxiliaryInformation* /*info*/ = NULL)
70  : node(node)
71 #if 0 // [Robb Matzke 2021-03-17]: unused
72  , info(info)
73 #endif
74  {
75 #ifdef _MSC_VER
76 //#define __builtin_constant_p(exp) (0)
77 #endif
78  assert(node);
79  }
80  std::string toString() const;
81  // String for debugging graphs
82  std::string toStringForDebugging() const;
83  // ID to use for Dot, etc.
84  std::string id() const;
85 
86  SgAsmInstruction *getNode() const {
87  return node;
88  }
89 
90  std::vector<CFGEdge> outEdges() const;
91  std::vector<CFGEdge> inEdges() const;
92  bool operator==(const CFGNode& o) const {
93  return node == o.node;
94  }
95  bool operator!=(const CFGNode& o) const {
96  return !(*this == o);
97  }
98  bool operator<(const CFGNode& o) const {
99  return node < o.node;
100  }
101  };
102 
103  class CFGEdge {
104  CFGNode src, tgt;
105 #if 0 // [Robb Matzke 2021-03-17]: unused
106  const AuxiliaryInformation *info;
107 #endif
108  public:
109  CFGEdge(CFGNode src, CFGNode tgt, const AuxiliaryInformation* /*info*/ = NULL)
110  : src(src), tgt(tgt)
111 #if 0 // [Robb Matzke 2021-03-17]: unused
112  , info(info)
113 #endif
114  {}
115  std::string toString() const; // Pretty string for Dot node labels, etc.
116  std::string toStringForDebugging() const; // String for debugging graphs
117  std::string id() const; // ID to use for Dot, etc.
118  CFGNode source() const {
119  return src;
120  }
121  CFGNode target() const {
122  return tgt;
123  }
124  EdgeConditionKind condition() const;
125  //SgExpression* caseLabel() const;
126  //SgExpression* conditionBasedOn() const;
127  //std::vector<SgInitializedName*> scopesBeingExited() const;
128  //std::vector<SgInitializedName*> scopesBeingEntered() const;
129  bool operator==(const CFGEdge& o) const {
130  return src == o.src && tgt == o.tgt;
131  }
132  bool operator!=(const CFGEdge& o) const {
133  return src != o.src || tgt != o.tgt;
134  }
135  bool operator<(const CFGEdge& o) const {
136  return src < o.src || (src == o.src && tgt < o.tgt);
137  }
138  };
139 
140  // Used in inEdges() and outEdges() methods
141  void makeEdge(SgAsmInstruction *from, SgAsmInstruction *to, const AuxiliaryInformation *info, std::vector<CFGEdge> &result);
142 }
143 
144 #endif
145 #endif
Base class for machine instructions.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846
SgAsmInstruction * getInstructionAtAddress(rose_addr_t addr) const
Returns the instruction (if any) disassembled at the specified address.
Definition: virtualBinCFG.h:43
Base class for statement-like subclasses.