RoseBin_DataFlowAbstract.h

Go to the documentation of this file.
00001 /****************************************************
00002  * RoseBin :: Binary Analysis for ROSE
00003  * Author : tps
00004  * Date : Sep7 07
00005  * Decription : Data flow Analysis
00006  ****************************************************/
00007 
00008 #ifndef __RoseBin_DataFlowAbstract__
00009 #define __RoseBin_DataFlowAbstract__
00010 
00011 #include <stdio.h>
00012 #include <iostream>
00013 #include "RoseBin_Graph.h"
00014 //#include "RoseBin.h"
00015 #include "GraphAlgorithms.h"
00016 
00017 class RoseBin_Variable  {
00018  public:
00019   //remove later!
00020   //  typedef rose_hash::unordered_map <std::string, SgGraphNode*,rose_hash::hash_string,rose_hash::eqstr_string> nodeType;
00021   //typedef rose_graph_node_edge_hash_multimap edgeType;
00022   //  typedef rose_graph_hash_multimap nodeType;
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 { //: public GraphAlgorithms {
00070  protected:
00071    rose_hash::unordered_map <uint64_t, RoseBin_Variable*> variables;
00072    GraphAlgorithms* g_algo;
00073 // CH (4/9/2010): Use boost::unordered instead
00074 //#ifdef _MSC_VER
00075 #if 0
00076 //   typedef rose_hash::unordered_map <std::string, uint64_t,rose_hash::hash_string> variablesReverseType;
00077    typedef rose_hash::unordered_map <std::string, uint64_t> variablesReverseType;
00078 #else
00079    // CH (4/13/2010): Use boost::hash<string> instead
00080    //typedef rose_hash::unordered_map <std::string, uint64_t,rose_hash::hash_string,rose_hash::eqstr_string> variablesReverseType;
00081    typedef rose_hash::unordered_map <std::string, uint64_t> variablesReverseType;
00082 #endif
00083 
00084 // rose_hash::unordered_map <std::string, uint64_t> variablesReverse;
00085    variablesReverseType variablesReverse;
00086 
00087    rose_hash::unordered_map <uint64_t, RoseBin_Variable*> memory;
00088 
00089 
00090 
00091   // definition of def-use data-structures.
00092   // will need those for other analyses
00093   typedef std::multimap< std::pair<X86RegisterClass, int>, SgGraphNode*> multitype;
00094   //typedef std::map< SgGraphNode*, multitype> tabletype;
00095   //typedef __gnu_cxx::hash_multimap< std::pair<X86RegisterClass, int> , SgGraphNode*> multitype;
00096 // CH (4/9/2010): Use boost::unordered instead
00097 //#ifdef _MSC_VER
00098 #if 0
00099 //  typedef rose_hash::unordered_map< SgGraphNode*, multitype,rose_hash::hash_graph_node> tabletype;
00100         // tps (12/07/2009) This seemed to work before with the above line, hmm..
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   // statistics
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 // DQ (5/9/2009): Move a (two) non-pure-virtual out-of-line virtual member functions to be 
00119 // defined first in the class so that the vtable will be put into the translation unit where
00120 // these are defined (this avoid the error: undefined reference to `vtable for RoseBin_DefUseAnalysis'. 
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 

Generated on Wed May 16 06:18:11 2012 for ROSE by  doxygen 1.4.7