00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __RoseBin_FlowAnalysis__
00009 #define __RoseBin_FlowAnalysis__
00010
00011
00012 #include <stdio.h>
00013 #include <iostream>
00014
00015
00016
00017 #include "MyAstAttribute.h"
00018
00019
00020
00021
00022 #include <cstdlib>
00023 #include "GraphAlgorithms.h"
00024
00025
00026
00027
00028
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
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 {
00111 public:
00112
00113
00114
00115
00116 protected:
00117 rose_hash::unordered_map <uint64_t, SgAsmInstruction* > rememberInstructions;
00118
00119 typedef rose_hash::unordered_map< uint64_t, SgGraphNode*> tabletype_inv;
00120
00121
00122 tabletype_inv deftable_instr;
00123
00124 int nrOfFunctions;
00125
00126
00127
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
00136 std::string analysisName;
00137
00138
00139 std::string typeNode;
00140 std::string typeEdge;
00141
00142
00143 SgAsmFunction* funcDecl;
00144 SgGraphNode* funcDeclNode;
00145
00146
00147
00148
00149 std::stack <SgAsmInstruction*> worklist_forthisfunction;
00150
00151
00152
00153
00154
00155
00156 #if 0
00157
00158 rose_hash::unordered_map <std::string, SgAsmInstruction*> local_visited;
00159 #else
00160
00161
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
00169 rose_hash::unordered_map <std::string, SgIncidenceDirectedGraph*> graphs;
00170
00171 static bool initialized;
00172
00173
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
00191
00192
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
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
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