00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __RoseBin_DataFlowAbstract__
00009 #define __RoseBin_DataFlowAbstract__
00010
00011 #include <stdio.h>
00012 #include <iostream>
00013 #include "RoseBin_Graph.h"
00014
00015 #include "GraphAlgorithms.h"
00016
00017 class RoseBin_Variable {
00018 public:
00019
00020
00021
00022
00023
00024 private:
00025 uint64_t position;
00026 std::string name;
00027 RoseBin_DataTypes::DataTypes type;
00028 std::string description;
00029 int length;
00030 std::vector<uint64_t> value;
00031 public:
00032 RoseBin_Variable(uint64_t pos,
00033 std::string n,
00034 RoseBin_DataTypes::DataTypes t, std::string d, int l,
00035 std::vector<uint64_t> v) {
00036 position=pos;
00037 name =n;
00038 type =t;
00039 description=d;
00040 length=l;
00041 value = v;
00042 }
00043
00044 RoseBin_DataTypes::DataTypes getType() {
00045 return type;
00046 }
00047
00048 int getLength() { return length;}
00049 std::string getName() { return name;}
00050
00051 std::string toString() {
00052 std::string values="";
00053 std::vector<uint64_t>::iterator it = value.begin();
00054 for (;it!=value.end();++it) {
00055 uint64_t var_int = *it;
00056 std::string var_str = RoseBin_support::HexToString(var_int);
00057 values += ""+ var_str + " ";
00058 }
00059 std::string val = "("+RoseBin_support::HexToString(position)+") "+name+"("
00060 +description+ ") ["+
00061 RoseBin_support::getTypeName(type)+" "+
00062 RoseBin_support::ToString(length)+" Values: ("+values+")] ";
00063 return val;
00064 }
00065 };
00066
00067
00068
00069 class RoseBin_DataFlowAbstract {
00070 protected:
00071 rose_hash::unordered_map <uint64_t, RoseBin_Variable*> variables;
00072 GraphAlgorithms* g_algo;
00073
00074
00075 #if 0
00076
00077 typedef rose_hash::unordered_map <std::string, uint64_t> variablesReverseType;
00078 #else
00079
00080
00081 typedef rose_hash::unordered_map <std::string, uint64_t> variablesReverseType;
00082 #endif
00083
00084
00085 variablesReverseType variablesReverse;
00086
00087 rose_hash::unordered_map <uint64_t, RoseBin_Variable*> memory;
00088
00089
00090
00091
00092
00093 typedef std::multimap< std::pair<X86RegisterClass, int>, SgGraphNode*> multitype;
00094
00095
00096
00097
00098 #if 0
00099
00100
00101 typedef rose_hash::unordered_map< SgGraphNode*, multitype> tabletype;
00102 #else
00103 typedef rose_hash::unordered_map< SgGraphNode*, multitype,rose_hash::hash_graph_node,rose_hash::eqstr_graph_node> tabletype;
00104 #endif
00105
00106
00107 int nrOfMemoryWrites;
00108 int nrOfRegisterWrites;
00109
00110 std::set < SgGraphNode* >
00111 getAnyFor(const multitype* multi, std::pair<X86RegisterClass, int> initName);
00112
00113 public:
00114 tabletype deftable;
00115 tabletype usetable;
00116 RoseBin_Graph* vizzGraph;
00117
00118
00119
00120
00121 SgGraphNode* getPredecessor(SgGraphNode* node);
00122 SgGraphNode* getSuccessor(SgGraphNode* node);
00123
00124 RoseBin_DataFlowAbstract(GraphAlgorithms* algo) {g_algo=algo;}
00125 virtual ~RoseBin_DataFlowAbstract() {}
00126
00127 virtual bool run(std::string& name, SgGraphNode* node,SgGraphNode* before ) =0;
00128 virtual bool runEdge(SgGraphNode* node, SgGraphNode* next)=0;
00129 virtual void init(RoseBin_Graph* vg)=0;
00130
00131 int getDefinitionSize() {
00132 return deftable.size();
00133 }
00134 int getUsageSize() { return usetable.size();}
00135
00136 int64_t check_isRegister(SgGraphNode* node,
00137 SgAsmx86Instruction* inst,
00138 std::pair<X86RegisterClass, int> codeSearch,
00139 bool rightSide,
00140 std::vector<std::pair<X86RegisterClass, int> >& regsOfInterest,
00141 bool& cantTrack);
00142
00143
00144
00145 int64_t check_isLeftSideRegister(SgAsmx86Instruction* inst,
00146 std::pair<X86RegisterClass, int> codeSearch);
00147
00148 uint64_t getValueInExpression(SgAsmValueExpression* valExp);
00149
00150 int64_t trackValueForRegister(
00151 SgGraphNode* node,
00152 std::pair<X86RegisterClass, int> codeSearch,
00153 bool& cantTrack,
00154 SgAsmx86RegisterReferenceExpression* refExpr_rightHand);
00155
00156
00157 std::pair<X86RegisterClass, int>
00158 check_isRegister(SgGraphNode* node, SgAsmx86Instruction* inst,
00159 bool rightSide, bool& memoryReference, bool& registerReference );
00160
00161 SgAsmExpression* getOperand(SgAsmx86Instruction* inst,
00162 bool rightSide );
00163
00164 uint64_t getValueInMemoryRefExp(SgAsmExpression* ref);
00165
00166 bool isInstructionAlteringOneRegister(SgAsmx86Instruction* inst);
00167 bool altersMultipleRegisters(std::vector<std::pair<X86RegisterClass, int> >& codes,
00168 SgAsmx86Instruction* inst);
00169
00170 bool sameParents(SgGraphNode* node, SgGraphNode* next);
00171
00172 void printDefTableToFile(std::string file);
00173
00174 std::set < SgGraphNode* >
00175 getDefFor(SgGraphNode* node, std::pair<X86RegisterClass, int> initName) ;
00176
00177 std::set < SgGraphNode* >
00178 getUseFor(SgGraphNode* node, std::pair<X86RegisterClass, int> initName);
00179
00180
00181 const std::multimap < std::pair<X86RegisterClass, int> , SgGraphNode* >&
00182 getDefMultiMapFor(SgGraphNode* node);
00183
00184 const std::multimap< std::pair<X86RegisterClass, int> , SgGraphNode* > &
00185 getUseMultiMapFor(SgGraphNode* node);
00186
00187 uint64_t getValueOfInstr( SgAsmx86Instruction* inst, bool rightSide );
00188
00189
00190 RoseBin_Variable* createVariable(uint64_t position,
00191 std::vector<uint64_t> pos,
00192 std::string name, RoseBin_DataTypes::DataTypes type, std::string description,
00193 int length,
00194 std::vector<uint64_t> value,
00195 bool memoryRef);
00196
00197 RoseBin_Variable* getVariable(uint64_t pos);
00198 RoseBin_Variable* getVariable(std::string var);
00199
00200 };
00201
00202 #endif
00203