forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryCFGTraversalTutorial.C
77 lines (54 loc) · 2.13 KB
/
binaryCFGTraversalTutorial.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <rose.h>
#include <string>
#include <err.h>
#include "graphProcessing.h"
#include <Rose/BinaryAnalysis/ControlFlow.h>
using namespace std;
// you need boost to put together the graph
using namespace boost;
using namespace Rose;
//renaming to Vertex and Edge to save space
typedef boost::graph_traits<BinaryAnalysis::ControlFlow::Graph>::vertex_descriptor Vertex; /**< Graph vertex type. */
typedef boost::graph_traits<BinaryAnalysis::ControlFlow::Graph>::edge_descriptor Edge; /**< Graph edge type. */
//visitor class, analyze path runs on the paths as they are completed, it is declared in the graphProcessing.h as a virtual function
//that will be implemented by the user
class visitorTraversal : public SgGraphTraversal<BinaryAnalysis::ControlFlow::Graph>
{
public:
int pths;
int tltnodes;
virtual void analyzePath( vector<Vertex>& pth);
};
//The path analysis function, relatively self explanatory
void visitorTraversal::analyzePath(vector<Vertex>& pth) {
tltnodes += pth.size();
pths++;
}
int main(int argc, char *argv[]) {
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
/* Parse the binary file */
SgProject *project = frontend(argc, argv);
std::vector<SgAsmInterpretation*> interps = SageInterface::querySubTree<SgAsmInterpretation>(project);
if (interps.empty()) {
fprintf(stderr, "no binary interpretations found\n");
exit(1);
}
/* Calculate plain old CFG. */
BinaryAnalysis::ControlFlow cfg_analyzer;
BinaryAnalysis::ControlFlow::Graph* cfg = new BinaryAnalysis::ControlFlow::Graph;
cfg_analyzer.build_block_cfg_from_ast(interps.back(), *cfg);
//instantiating the graph visitor
visitorTraversal* vis = new visitorTraversal;
//setting object variables
vis->tltnodes = 0;
vis->pths = 0;
//the algorithm
vis->constructPathAnalyzer(cfg);
//printing object variables
std::cout << "pths: " << vis->pths << std::endl;
std::cout << "tltnodes: " << vis->tltnodes << std::endl;
return 0;
}