-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpchart.cpp
273 lines (208 loc) · 4.48 KB
/
pchart.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Patchdiff2
Portions (C) 2010 - 2011 Nicolas Pouvesle
Portions (C) 2007 - 2009 Tenable Network Security, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precomp.hpp"
#include "pchart.hpp"
#include "patchdiff.hpp"
#include "x86.hpp"
using namespace std;
extern cpu_t patchdiff_cpu;
ea_t get_fake_jump(ea_t ea)
{
switch(patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
return x86_get_fake_jump(ea);
default:
return BADADDR;
}
}
bool is_end_block(ea_t ea)
{
switch(patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
return x86_is_end_block(ea);
default:
return false;
}
}
ea_t get_direct_jump(ea_t ea)
{
xrefblk_t xb;
cref_t cr;
flags_t f = getFlags(ea);
bool b = xb.first_from(ea, XREF_FAR);
if (!b) return BADADDR;
cr = (cref_t)xb.type;
if (!xb.iscode || !(cr == fl_JF || cr == fl_JN || cr == fl_F) || (f & FF_JUMP)) return BADADDR;
switch(patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
if (x86_is_direct_jump(ea)) return xb.to;
default:
return BADADDR;
}
}
bool pflow_chart_t::getJump(func_t * fct, qvector<ea_t> & list, pbasic_block_t & bl)
{
xrefblk_t xb;
cref_t cr;
bool b, j, flow;
qvector<pedge_t> tmp;
qvector<pedge_t>::iterator pos;
ea_t tea, ea = bl.endEA, end, jaddr;
flags_t f;
size_t k;
int type = 0;
int cond;
j = flow = false;
end = get_item_end(ea);
b = xb.first_from(ea, XREF_ALL);
f = getFlags(ea);
cond = x86_is_cond_jump_pos(ea);
while (b)
{
cr = (cref_t)xb.type;
if (xb.iscode && (cr == fl_JF || cr == fl_JN || cr == fl_F))
{
pedge_t ed;
if (cr == fl_JF || cr == fl_JN) {
j = true;
type = 1;
} else if (! (f & FF_JUMP)) {
flow = true;
type = 2;
} else {
flow = false;
type = 3;
}
if (patchdiff_cpu == CPU_X8632 || patchdiff_cpu == CPU_X8664 || get_func_chunknum(fct, xb.to) >= 0)
{
jaddr = get_direct_jump(xb.to);
if (jaddr == BADADDR)
ed.ea = xb.to;
else
ed.ea = jaddr;
ed.type = type;
pos = tmp.end();
if (patchdiff_cpu == CPU_X8632 || patchdiff_cpu == CPU_X8664)
{
if ( (cond == 1 && cr == fl_F) || (cond == 2 && cr != fl_F) )
pos = tmp.begin();
}
else if (ed.ea == end)
pos = tmp.begin();
// TODO: We hit this assert.
assert(ed.type != 0);
tmp.insert(pos, ed);
}
}
b = xb.next_from();
}
tea = get_fake_jump(ea);
if (tea != BADADDR)
{
pedge_t ed;
j = true;
ed.ea = tea;
ed.type = 1;
}
if (j)
{
for (k=0; k<tmp.size(); k++)
{
pedge_t ed;
ed.ea = tmp[k].ea;
if (flow)
ed.type = tmp[k].type;
else
ed.type = 3;
if (xb.to != bl.startEA)
list.push_back(tmp[k].ea);
bl.succ.push_back(ed);
}
return true;
}
return false;
}
bool pflow_chart_t::check_address(ea_t ea)
{
qvector<pbasic_block_t>::iterator it;
for (it=blocks.begin(); it<blocks.end(); it++)
{
if (it->startEA == ea)
return true;
if (ea > it->startEA && ea < it->endEA)
{
pbasic_block_t bl;
pedge_t ed;
bl.startEA = ea;
bl.endEA = it->endEA;
bl.succ = it->succ;
it->endEA = ea;
it->succ.clear();
ed.ea = ea;
ed.type = 3;
it->succ.push_back(ed);
blocks.push_back(bl);
return true;
}
}
return false;
}
pflow_chart_t::pflow_chart_t(func_t * fct)
{
ea_t ea;
qvector<ea_t> to_trace;
bool cont;
flags_t f;
to_trace.push_back(fct->startEA);
while (!to_trace.empty())
{
ea = to_trace.front();
to_trace.erase(to_trace.begin());
if (check_address(ea))
continue;
pbasic_block_t bl;
bl.startEA = ea;
bl.endEA = ea;
cont = true;
while(cont)
{
ea = bl.endEA;
f = getFlags(ea);
if ( (!isFlow(f) && (ea != bl.startEA)) || !isCode(f) )
break;
if ( check_address(ea) )
{
pedge_t ed;
ed.ea = ea;
ed.type = 3;
bl.succ.push_back(ed);
break;
}
if ( getJump(fct, to_trace, bl) )
cont = false;
if ( is_end_block(ea) )
break;
bl.endEA = get_item_end(ea);
}
blocks.push_back(bl);
}
nproper = blocks.size();
}