File: graph.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (196 lines) | stat: -rw-r--r-- 4,595 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* radare - LGPL - Copyright 2007-2012 - pancake */
/* graph with stack facilities implementation */

#include <r_util.h>

#if 0
RGraph *r_anal_getgraph(RAnal *anal, ut64 addr) {
        RGraph *g;
        RFunction *f = r_anal_fcn_get (anal, addr);
        if (!f) return NULL;
        g = r_graph_new ();
        // walk basic blocks, and create nodes and edges
}

// r_anal_graph_to_kv()
node.0x804840={"name":"patata",size:123,"code":"jlasdfjlksf"}
node.0x804840.to=[0x804805,0x804805,0x0485085,0x90850]
#endif

R_API RGraphNode *r_graph_node_new (ut64 addr, void *data) {
	RGraphNode *p = R_NEW0 (RGraphNode);
	p->parents = r_list_new ();
	p->children = r_list_new ();
	p->addr = addr;
	p->data = data;
	p->refs = 0;
	return p;
}

R_API void r_graph_node_free (RGraphNode *n) {
	r_list_free (n->parents);
	r_list_free (n->children);
	if (n->free)
		n->free (n->data);
	free (n);
}

static void walk_children (RGraph *t, RGraphNode *tn, int level) {
	int i;
	RListIter *iter;
	RGraphNode *n;
	if (r_list_contains (t->path, tn)) {
		// do not repeat pushed nodes
		return;
	}
	for (i=0; i<level; i++) 
		t->printf ("   ");
	t->printf (" 0x%08"PFMT64x" refs %d\n",
			tn->addr, tn->refs);
	r_list_foreach (tn->parents, iter, n) {
		for (i=0; i<level; i++) 
			t->printf ("   ");
		t->printf (" |_ 0x%08"PFMT64x"\n", n->addr);
	}
	r_list_push (t->path, tn);
	r_list_foreach (tn->children, iter, n) {
		walk_children (t, n, level+1);
	}
	r_list_pop (t->path);
} 

R_API void r_graph_traverse(RGraph *t) {
	RListIter *iter;
	RGraphNode *root;
	RList *path = t->path;
	t->path = r_list_new ();
	r_list_foreach (t->roots, iter, root) {
		walk_children (t, root, 0);
	}
	r_list_free (t->path);
	t->path = path;
}

R_API RGraph* r_graph_new () {
	RGraph *t = R_NEW0 (RGraph);
	t->printf = (PrintfCallback) printf;
	t->path = r_list_new ();
	t->nodes = r_list_new ();
	t->roots = r_list_new ();
	t->nodes->free = (RListFree)r_graph_node_free;
	t->root = NULL;
	t->cur = NULL;
	return t;
}

R_API void r_graph_free (RGraph* t) {
	r_list_free (t->nodes);
	r_list_free (t->path);
	free (t);
}

R_API RGraphNode* r_graph_get_current (RGraph *t, ut64 addr) {
	return t->cur? t->cur->data: NULL;
}

R_API RGraphNode* r_graph_get_node (RGraph *t, ut64 addr, boolt c) {
	RListIter *iter;
	RGraphNode *n;
	r_list_foreach (t->nodes, iter, n) {
		if (n->addr == addr)
			return n;
	}
	if (c) {
		n = r_graph_node_new (addr, NULL);
		r_list_append (t->nodes, n);
		return n;
	}
	return NULL;
}

R_API void r_graph_reset (RGraph *t) {
	r_list_free (t->nodes);
	t->nodes = r_list_new ();
	t->nodes->free = (RListFree)r_graph_node_free;
	r_list_free (t->roots);
	t->roots = r_list_new ();
	t->root = NULL;
}

// g.add (0x804840, 0x804408, null);
R_API void r_graph_add (RGraph *t, ut64 from, ut64 addr, void *data) {
	RGraphNode *n, *f = r_graph_get_node (t, from, R_TRUE);
	n = r_graph_get_node (t, addr, R_TRUE);
	n->data = data;
	if (!r_list_contains (f->children, n))
		r_list_append (f->children, n);
	if (!r_list_contains (f->parents, n))
		r_list_append (n->parents, f);
}

// plant: to set new root :)
R_API void r_graph_plant(RGraph *t) {
	t->root = NULL;
}

R_API void r_graph_push (RGraph *t, ut64 addr, void *data) {
	RGraphNode *c, *n = r_graph_get_node (t, addr, R_FALSE);
	t->level++;
	if (!n) {
		n = r_graph_node_new (addr, data);
		r_list_append (t->nodes, n);
		if (t->root == NULL) {
			t->root = n;
			r_list_append (t->roots, n);
		}
	} else {
		n->refs++;
		n->data = data; // update data if already pushed?
	}
	if (!t->cur)
		t->cur = r_list_contains (t->nodes, n);
	c = t->cur->data;
	if (!r_list_contains (c->children, n))
		r_list_append (c->children, n);
	if (c->addr && !r_list_contains (n->parents, c))
		r_list_append (n->parents, c);
	t->cur = r_list_append (t->path, n);
}

R_API RGraphNode* r_graph_pop(RGraph *t) {
	RListIter *p;
	if (!t || !t->path || !t->cur)
		return NULL;
	// TODO: handle null
	t->level--;
	if (t->level<0) {
		eprintf ("Negative pop!\n");
		return NULL;
	}
	p = t->cur->p;
	r_list_delete (t->path, t->cur);
	t->cur = p;
	return (RGraphNode*)t->cur;
}

#if TEST
// usage
main () {
	RGraph *t = r_graph_new ();
	r_graph_push (t, 0x8048590, NULL);
	r_graph_push (t, 0x8048230, NULL);
	r_graph_pop (t);
	r_graph_push (t, 0x8044300, NULL);
	r_graph_push (t, 0x8046300, NULL);
	r_graph_pop (t);
	r_graph_push (t, 0x8046388, NULL);
	r_graph_pop (t);
	r_graph_push (t, 0x8046388, NULL);
	r_graph_push (t, 0x8046388, NULL);
		r_graph_push (t, 0x8046300, NULL);
		r_graph_push (t, 0x8046300, NULL);
	r_graph_pop (t);
	r_graph_traverse (t);
	r_graph_free (t);
}
#endif