RoseBin_FlowAnalysis.h

Go to the documentation of this file.
00001 /****************************************************
00002  * RoseBin :: Binary Analysis for ROSE
00003  * Author : tps
00004  * Date : Jul27 07
00005  * Decription : Control flow Analysis
00006  ****************************************************/
00007 
00008 #ifndef __RoseBin_FlowAnalysis__
00009 #define __RoseBin_FlowAnalysis__
00010 
00011 //#include <mysql.h>
00012 #include <stdio.h>
00013 #include <iostream>
00014 //#include "sage3.h"
00015 
00016 //#include "RoseBin_support.h"
00017 #include "MyAstAttribute.h"
00018 //#include "RoseBin_unparse_visitor.h"
00019 //#include "../graph/RoseBin_DotGraph.h"
00020 //#include "../graph/RoseBin_GmlGraph.h"
00021 
00022 #include <cstdlib>
00023 #include "GraphAlgorithms.h"
00024 
00025 //typedef rose_graph_node_edge_hash_multimap edgeType;
00026 
00027 
00028 // **************** AS DEFINED BY ANDREAS *****************************************
00029 class FindAsmFunctionsVisitor: public std::binary_function<SgNode*, std::vector<SgAsmFunction *>* , void* >
00030 {
00031   public:
00032     void* operator()(first_argument_type node, std::vector<SgAsmFunction*>* insns ) const{
00033       if (isSgAsmFunction(node)) insns->push_back(isSgAsmFunction(node));
00034       return NULL;
00035     }
00036 };
00037 
00038 class FindSgFunctionsVisitor: public std::binary_function<SgNode*, std::vector<SgFunctionDeclaration *>* , void* >
00039 {
00040   public:
00041     void* operator()(first_argument_type node, std::vector<SgFunctionDeclaration*>* insns ) const{
00042       if (isSgFunctionDeclaration(node)) insns->push_back(isSgFunctionDeclaration(node));
00043       return NULL;
00044     }
00045 };
00046 
00047 
00048 class FindInstructionsVisitor: public std::binary_function<SgNode*, std::vector<SgAsmInstruction *>* , void* >
00049 {
00050  public:
00051   void* operator()(first_argument_type node, std::vector<SgAsmInstruction*>* insns ) const{
00052     if (isSgAsmInstruction(node)) insns->push_back(isSgAsmInstruction(node));
00053     return NULL;
00054   }
00055 };
00056 
00057 
00058 class FindInstructionsVisitorx86: public std::binary_function<SgNode*, std::vector<SgAsmx86Instruction *>* , void* >
00059 {
00060  public:
00061   void* operator()(first_argument_type node, std::vector<SgAsmx86Instruction*>* insns ) const{
00062     if (isSgAsmx86Instruction(node)) insns->push_back(isSgAsmx86Instruction(node));
00063     return NULL;
00064   }
00065 };
00066 
00067 class FindAsmStatementsVisitor: public std::binary_function<SgNode*, std::vector<SgAsmStatement *>* , void* >
00068 {
00069  public:
00070   void* operator()(first_argument_type node, std::vector<SgAsmStatement*>* insns ) const{
00071     if (isSgAsmStatement(node)) insns->push_back(isSgAsmStatement(node));
00072     return NULL;
00073   }
00074 };
00075 
00076 class FindAsmStatementsHeaderVisitor: public std::binary_function<SgNode*, std::vector<SgAsmNode *>* , void* >
00077 {
00078  public:
00079   void* operator()(first_argument_type node, std::vector<SgAsmNode*>* insns ) const{
00080     if (isSgAsmStatement(node)) insns->push_back(isSgAsmStatement(node));
00081     if (isSgAsmExecutableFileFormat(node)) insns->push_back(isSgAsmExecutableFileFormat(node));
00082     return NULL;
00083   }
00084 };
00085 
00086 class FindStatementsVisitor: public std::binary_function<SgNode*, std::vector<SgStatement *>* , void* >
00087 {
00088  public:
00089   void* operator()(first_argument_type node, std::vector<SgStatement*>* insns ) const{
00090     if (isSgStatement(node))
00091       //      if (!isSgStatement(node)->get_file_info()->isCompilerGenerated())
00092         insns->push_back(isSgStatement(node));
00093         //}
00094     return NULL;
00095   }
00096 };
00097 
00098 class FindNodeVisitor: public std::binary_function<SgNode*, std::vector<SgLocatedNode *>* , void* >
00099 {
00100  public:
00101   void* operator()(first_argument_type node, std::vector<SgLocatedNode*>* insns ) const{
00102     if (isSgNode(node))
00103       insns->push_back(isSgLocatedNode(node));
00104     return NULL;
00105   }
00106 };
00107 
00108 // ************************************************************************************
00109 
00110 class RoseBin_FlowAnalysis : public AstSimpleProcessing {//, public GraphAlgorithms {
00111  public:
00112   //remove later!
00113    //typedef rose_hash::unordered_map <std::string, SgGraphNode*,rose_hash::hash_string,rose_hash::eqstr_string> nodeType;
00114 
00115 
00116  protected:
00117    rose_hash::unordered_map <uint64_t, SgAsmInstruction* > rememberInstructions; // Insn address -> ROSE insn
00118 
00119   typedef rose_hash::unordered_map< uint64_t, SgGraphNode*> tabletype_inv;
00120 
00121   //tabletype_inv usetable_instr;
00122   tabletype_inv deftable_instr;
00123 
00124   int nrOfFunctions;
00125 
00126   //typedef SB_DirectedGraph::nodeType nodeType;
00127   //typedef rose_hash::unordered_map < std::string, SgDirectedGraphEdge*,rose_hash::hash_string,rose_hash::eqstr_string> edgeType;
00128 
00129   SgAsmNode* globalBin;
00130   int func_nr;
00131   int nr_target_missed;
00132   RoseBin_Graph* vizzGraph;
00133   std::string fileName;
00134   bool printEdges;
00135   // the name of the analysis
00136   std::string analysisName;
00137 
00138   // the string types of nodes and edges
00139   std::string typeNode;
00140   std::string typeEdge;
00141 
00142   // needed for CallGraphAnalysis
00143   SgAsmFunction* funcDecl;
00144   SgGraphNode* funcDeclNode;
00145 
00146 
00147 
00148   // worklist to build the CFG graph
00149   std::stack <SgAsmInstruction*> worklist_forthisfunction;
00150 
00151   // visited map for the CFG graph
00152 // DQ (4/23/2009): We need to specify the default template parameters explicitly.
00153 // rose_hash::unordered_map <std::string, SgAsmInstruction*> local_visited;
00154 // CH (4/9/2010): Use boost::unordered instead
00155 //#ifdef _MSC_VER
00156 #if 0
00157 //  rose_hash::unordered_map <std::string, SgAsmInstruction*,rose_hash::hash_string> local_visited;
00158   rose_hash::unordered_map <std::string, SgAsmInstruction*> local_visited;
00159 #else
00160   // CH (4/13/2010): Use boost::hash<string> instead
00161   //rose_hash::unordered_map <std::string, SgAsmInstruction*,rose_hash::hash_string,rose_hash::eqstr_string> local_visited;
00162   rose_hash::unordered_map <std::string, SgAsmInstruction*> local_visited;
00163 #endif
00164 
00165   typedef std::map<std::string, SgAsmFunction*> bin_funcs_type;
00166   bin_funcs_type bin_funcs;
00167 
00168   // vector of graphs
00169   rose_hash::unordered_map <std::string, SgIncidenceDirectedGraph*> graphs;
00170 
00171   static bool initialized;
00172 
00173   //VirtualBinCFG::AuxiliaryInformation* info;
00174 
00175   void initFunctionList(SgAsmNode* global);
00176   void process_jumps();
00177   SgAsmInstruction* process_jumps_get_target(SgAsmx86Instruction* inst);
00178   void resolveFunctions(SgAsmNode* global);
00179   SgAsmInstruction* resolveFunction(SgAsmInstruction* inst, bool hasStopCondition);
00180   void convertBlocksToFunctions(SgAsmNode* globalNode);
00181   void flattenBlocks(SgAsmNode* globalNode);
00182 
00183   bool db;
00184 
00185   int nrNodes;
00186   int nrEdges;
00187 
00188   GraphAlgorithms* g_algo;
00189  public:
00190   //  RoseBin* roseBin;
00191 
00192 // DQ (10/20/2010): Moved to source file to support compilation of language only mode which excludes binary analysis support.
00193   RoseBin_FlowAnalysis(SgAsmNode* global, GraphAlgorithms* algo);
00194 
00195   virtual ~RoseBin_FlowAnalysis() {}
00196 
00197   void setInitializedFalse() {
00198     initialized=false;
00199   }
00200   RoseBin_Graph* getGraph() {return vizzGraph;}
00201 
00202   bool forward_analysis;
00203   void printAST(SgAsmNode* globalNode);
00204   // run this analysis
00205   virtual void run(RoseBin_Graph* vg, std::string fileN, bool multiedge) =0;
00206 
00207   std::string getName() { return analysisName;}
00208 
00209   void visit(SgNode* node);
00210   void checkControlFlow( SgAsmInstruction* binInst,
00211                          int functionSize, int countDown,
00212                          std::string& currentFunctionName, int func_nr);
00213 
00214   SgGraphNode*
00215     getNodeFor(uint64_t inst) { return deftable_instr[inst];}
00216 
00217 
00218   void createInstToNodeTable();
00219   uint64_t getAddressForNode(SgGraphNode* node);
00220 
00221   // converts string to hex
00222   template <class T>
00223     bool from_string(T& t,
00224                      const std::string& s,
00225                      std::ios_base& (*f)(std::ios_base&))
00226     {
00227       std::istringstream iss(s);
00228       return !(iss >> f >> t).fail();
00229     }
00230 
00231   int nodesVisited() {
00232     return nrNodes;
00233   }
00234 
00235   int edgesVisited() {
00236     return nrEdges;
00237   }
00238 
00239   bool sameParents(SgGraphNode* node, SgGraphNode* next);
00240   void getRootNodes(std::vector <SgGraphNode*>& rootNodes);
00241 
00242 
00243   SgGraphNode* addCFNode(std::string& name, std::string& type, int address, bool isFunction, SgNode* int_node);
00244 
00245   void clearMaps();
00246 
00247 };
00248 
00249 #endif
00250 

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