-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
branchinfo.c
321 lines (280 loc) · 9.32 KB
/
branchinfo.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2019 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to the 2-Clause BSD license which is |
| available through the LICENSE file, or online at |
| http://opensource.org/licenses/bsd-license.php |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: branch_info.c,v 1.1 2006-09-26 09:40:26 derick Exp $ */
#include <stdlib.h>
#include <math.h>
#include "branchinfo.h"
ZEND_EXTERN_MODULE_GLOBALS(vld)
vld_branch_info *vld_branch_info_create(unsigned int size)
{
vld_branch_info *tmp;
tmp = calloc(1, sizeof(vld_branch_info));
tmp->size = size;
tmp->branches = calloc(size, sizeof(vld_branch));
tmp->entry_points = vld_set_create(size);
tmp->starts = vld_set_create(size);
tmp->ends = vld_set_create(size);
tmp->paths_count = 0;
tmp->paths_size = 0;
tmp->paths = NULL;
return tmp;
}
void vld_branch_info_free(vld_branch_info *branch_info)
{
unsigned int i;
for (i = 0; i < branch_info->paths_count; i++) {
free(branch_info->paths[i]->elements);
free(branch_info->paths[i]);
}
free(branch_info->paths);
free(branch_info->branches);
vld_set_free(branch_info->entry_points);
vld_set_free(branch_info->starts);
vld_set_free(branch_info->ends);
free(branch_info);
}
void vld_branch_info_update(vld_branch_info *branch_info, unsigned int pos, unsigned int lineno, unsigned int outidx, unsigned int jump_pos)
{
vld_set_add(branch_info->ends, pos);
if (branch_info->branches[pos].outs_count < VLD_BRANCH_MAX_OUTS) {
branch_info->branches[pos].outs[branch_info->branches[pos].outs_count] = jump_pos;
branch_info->branches[pos].outs_count++;
}
branch_info->branches[pos].start_lineno = lineno;
}
void vld_only_leave_first_catch(zend_op_array *opa, vld_branch_info *branch_info, int position)
{
unsigned int exit_jmp;
#if PHP_VERSION_ID >= 70300 && ZEND_USE_ABS_JMP_ADDR
zend_op *base_address = &(opa->opcodes[0]);
#endif
if (opa->opcodes[position].opcode == ZEND_FETCH_CLASS) {
position++;
}
if (opa->opcodes[position].opcode != ZEND_CATCH) {
return;
}
#if PHP_VERSION_ID >= 70300
if (!(opa->opcodes[position].extended_value & ZEND_LAST_CATCH)) {
exit_jmp = VLD_ZNODE_JMP_LINE(opa->opcodes[position].op2, position, base_address);
#else
if (!opa->opcodes[position].result.num) {
# if PHP_VERSION_ID >= 70100
exit_jmp = position + ((signed int) opa->opcodes[position].extended_value / sizeof(zend_op));
# else
exit_jmp = opa->opcodes[position].extended_value;
# endif
#endif
if (opa->opcodes[exit_jmp].opcode == ZEND_FETCH_CLASS) {
exit_jmp++;
}
if (opa->opcodes[exit_jmp].opcode == ZEND_CATCH) {
vld_only_leave_first_catch(opa, branch_info, exit_jmp);
}
}
vld_set_remove(branch_info->entry_points, position);
}
void vld_branch_post_process(zend_op_array *opa, vld_branch_info *branch_info)
{
unsigned int i;
int in_branch = 0, last_start = VLD_JMP_NOT_SET;
#if PHP_VERSION_ID >= 70300 && ZEND_USE_ABS_JMP_ADDR
zend_op *base_address = &(opa->opcodes[0]);
#endif
/* Figure out which CATCHes are chained, and hence which ones should be
* considered entry points */
for (i = 0; i < branch_info->entry_points->size; i++) {
if (vld_set_in(branch_info->entry_points, i) && opa->opcodes[i].opcode == ZEND_CATCH) {
#if PHP_VERSION_ID >= 70300
# if ZEND_USE_ABS_JMP_ADDR
# if PHP_VERSION_ID >= 80200
if (opa->opcodes[i].op2.jmp_addr != -1) {
# else
if (opa->opcodes[i].op2.jmp_addr != NULL) {
# endif
# else
if (opa->opcodes[i].op2.jmp_offset != 0) {
# endif
vld_only_leave_first_catch(opa, branch_info, VLD_ZNODE_JMP_LINE(opa->opcodes[i].op2, i, base_address));
}
#elif PHP_VERSION_ID >= 70100
vld_only_leave_first_catch(opa, branch_info, i + ((signed int) opa->opcodes[i].extended_value / sizeof(zend_op)));
#else
vld_only_leave_first_catch(opa, branch_info, opa->opcodes[i].extended_value);
#endif
}
}
for (i = 0; i < branch_info->starts->size; i++) {
if (vld_set_in(branch_info->starts, i)) {
if (in_branch) {
branch_info->branches[last_start].outs_count = 1;
branch_info->branches[last_start].outs[0] = i;
branch_info->branches[last_start].end_op = i-1;
branch_info->branches[last_start].end_lineno = branch_info->branches[i].start_lineno;
}
last_start = i;
in_branch = 1;
}
if (vld_set_in(branch_info->ends, i)) {
size_t j;
for (j = 0; j < branch_info->branches[i].outs_count; j++) {
branch_info->branches[last_start].outs[j] = branch_info->branches[i].outs[j];
}
branch_info->branches[last_start].outs_count = branch_info->branches[i].outs_count;
branch_info->branches[last_start].end_op = i;
branch_info->branches[last_start].end_lineno = branch_info->branches[i].start_lineno;
in_branch = 0;
}
}
}
static void vld_path_add(vld_path *path, unsigned int nr)
{
if (path->elements_count == path->elements_size) {
path->elements_size += 32;
path->elements = realloc(path->elements, sizeof(unsigned int) * path->elements_size);
}
path->elements[path->elements_count] = nr;
path->elements_count++;
}
static void vld_branch_info_add_path(vld_branch_info *branch_info, vld_path *path)
{
if (branch_info->paths_count == branch_info->paths_size) {
branch_info->paths_size += 32;
branch_info->paths = realloc(branch_info->paths, sizeof(vld_path*) * branch_info->paths_size);
}
branch_info->paths[branch_info->paths_count] = path;
branch_info->paths_count++;
}
static vld_path *vld_path_new(vld_path *old_path)
{
vld_path *tmp;
tmp = calloc(1, sizeof(vld_path));
if (old_path) {
unsigned i;
for (i = 0; i < old_path->elements_count; i++) {
vld_path_add(tmp, old_path->elements[i]);
}
}
return tmp;
}
static void vld_path_free(vld_path *path)
{
if (path->elements) {
free(path->elements);
}
free(path);
}
static unsigned int vld_branch_find_last_element(vld_path *path)
{
return path->elements[path->elements_count-1];
}
static int vld_path_exists(vld_path *path, unsigned int elem1, unsigned int elem2)
{
unsigned int i;
for (i = 0; i < path->elements_count - 1; i++) {
if (path->elements[i] == elem1 && path->elements[i + 1] == elem2) {
return 1;
}
}
return 0;
}
static void vld_branch_find_path(unsigned int nr, vld_branch_info *branch_info, vld_path *prev_path)
{
unsigned int last;
vld_path *new_path;
int found = 0;
size_t i = 0;
if (branch_info->paths_count > 255/*65535*/) {
return;
}
new_path = vld_path_new(prev_path);
vld_path_add(new_path, nr);
last = vld_branch_find_last_element(new_path);
for (i = 0; i < branch_info->branches[nr].outs_count; i++) {
int out = branch_info->branches[nr].outs[i];
if (out != 0 && out != VLD_JMP_EXIT && !vld_path_exists(new_path, last, out)) {
vld_branch_find_path(out, branch_info, new_path);
found = 1;
}
}
if (!found) {
vld_branch_info_add_path(branch_info, new_path);
} else {
vld_path_free(new_path);
}
}
void vld_branch_find_paths(vld_branch_info *branch_info)
{
unsigned int i;
for (i = 0; i < branch_info->entry_points->size; i++) {
if (vld_set_in(branch_info->entry_points, i)) {
vld_branch_find_path(i, branch_info, NULL);
}
}
}
void vld_branch_info_dump(zend_op_array *opa, vld_branch_info *branch_info)
{
unsigned int i, j;
const char *fname = opa->function_name ? ZSTRING_VALUE(opa->function_name) : "__main";
if (VLD_G(path_dump_file)) {
fprintf(VLD_G(path_dump_file), "subgraph cluster_%p {\n\tlabel=\"%s\";\n\tgraph [rankdir=\"LR\"];\n\tnode [shape = record];\n", opa, fname);
for (i = 0; i < branch_info->starts->size; i++) {
if (vld_set_in(branch_info->starts, i)) {
fprintf(
VLD_G(path_dump_file),
"\t\"%s_%d\" [ label = \"{ op #%d-%d | line %d-%d }\" ];\n",
fname, i, i,
branch_info->branches[i].end_op,
branch_info->branches[i].start_lineno,
branch_info->branches[i].end_lineno
);
if (vld_set_in(branch_info->entry_points, i)) {
fprintf(VLD_G(path_dump_file), "\t%s_ENTRY -> %s_%d\n", fname, fname, i);
}
for (j = 0; j < branch_info->branches[i].outs_count; j++) {
if (branch_info->branches[i].outs[j]) {
if (branch_info->branches[i].outs[j] == VLD_JMP_EXIT) {
fprintf(VLD_G(path_dump_file), "\t%s_%d -> %s_EXIT;\n", fname, i, fname);
} else {
fprintf(VLD_G(path_dump_file), "\t%s_%d -> %s_%d;\n", fname, i, fname, branch_info->branches[i].outs[j]);
}
}
}
}
}
fprintf(VLD_G(path_dump_file), "}\n");
}
for (i = 0; i < branch_info->starts->size; i++) {
if (vld_set_in(branch_info->starts, i)) {
printf("branch: #%3d; line: %5d-%5d; sop: %5d; eop: %5d",
i,
branch_info->branches[i].start_lineno,
branch_info->branches[i].end_lineno,
i,
branch_info->branches[i].end_op
);
for (j = 0; j < branch_info->branches[i].outs_count; j++) {
if (branch_info->branches[i].outs[j]) {
printf("; out%d: %3d", j, branch_info->branches[i].outs[j]);
}
}
printf("\n");
}
}
for (i = 0; i < branch_info->paths_count; i++) {
printf("path #%d: ", i + 1);
for (j = 0; j < branch_info->paths[i]->elements_count; j++) {
printf("%d, ", branch_info->paths[i]->elements[j]);
}
printf("\n");
}
}