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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
+----------------------------------------------------------------------+
| Zend JIT |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Dmitry Stogov <dmitry@php.net> |
| Xinchen Hui <laruence@php.net> |
+----------------------------------------------------------------------+
*/
#define HAVE_DISASM 1
#define DISASM_INTEL_SYNTAX 0
#include "jit/libudis86/itab.c"
#include "jit/libudis86/decode.c"
#include "jit/libudis86/syn.c"
#if DISASM_INTEL_SYNTAX
# include "jit/libudis86/syn-intel.c"
#else
# include "jit/libudis86/syn-att.c"
#endif
#include "jit/libudis86/udis86.c"
static void zend_jit_disasm_add_symbol(const char *name,
uint64_t addr,
uint64_t size);
#ifndef _WIN32
# include "jit/zend_elf.c"
#endif
#include "zend_sort.h"
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#ifndef _WIN32
#include <dlfcn.h>
#endif
static struct ud ud;
typedef struct _sym_node {
uint64_t addr;
uint64_t end;
struct _sym_node *parent;
struct _sym_node *child[2];
unsigned char info;
char name[1];
} zend_sym_node;
static zend_sym_node *symbols = NULL;
static void zend_syms_rotateleft(zend_sym_node *p) {
zend_sym_node *r = p->child[1];
p->child[1] = r->child[0];
if (r->child[0]) {
r->child[0]->parent = p;
}
r->parent = p->parent;
if (p->parent == NULL) {
symbols = r;
} else if (p->parent->child[0] == p) {
p->parent->child[0] = r;
} else {
p->parent->child[1] = r;
}
r->child[0] = p;
p->parent = r;
}
static void zend_syms_rotateright(zend_sym_node *p) {
zend_sym_node *l = p->child[0];
p->child[0] = l->child[1];
if (l->child[1]) {
l->child[1]->parent = p;
}
l->parent = p->parent;
if (p->parent == NULL) {
symbols = l;
} else if (p->parent->child[1] == p) {
p->parent->child[1] = l;
} else {
p->parent->child[0] = l;
}
l->child[1] = p;
p->parent = l;
}
static void zend_jit_disasm_add_symbol(const char *name,
uint64_t addr,
uint64_t size)
{
zend_sym_node *sym;
size_t len = strlen(name);
sym = malloc(sizeof(zend_sym_node) + len + 1);
if (!sym) {
return;
}
sym->addr = addr;
sym->end = (addr + size - 1);
memcpy((char*)&sym->name, name, len + 1);
sym->parent = sym->child[0] = sym->child[1] = NULL;
sym->info = 1;
if (symbols) {
zend_sym_node *node = symbols;
/* insert it into rbtree */
do {
if (sym->addr > node->addr) {
ZEND_ASSERT(sym->addr > (node->end));
if (node->child[1]) {
node = node->child[1];
} else {
node->child[1] = sym;
sym->parent = node;
break;
}
} else if (sym->addr < node->addr) {
if (node->child[0]) {
node = node->child[0];
} else {
node->child[0] = sym;
sym->parent = node;
break;
}
} else {
ZEND_ASSERT(sym->addr == node->addr);
free(sym);
return;
}
} while (1);
/* fix rbtree after instering */
while (sym && sym != symbols && sym->parent->info == 1) {
if (sym->parent == sym->parent->parent->child[0]) {
node = sym->parent->parent->child[1];
if (node && node->info == 1) {
sym->parent->info = 0;
node->info = 0;
sym->parent->parent->info = 1;
sym = sym->parent->parent;
} else {
if (sym == sym->parent->child[1]) {
sym = sym->parent;
zend_syms_rotateleft(sym);
}
sym->parent->info = 0;
sym->parent->parent->info = 1;
zend_syms_rotateright(sym->parent->parent);
}
} else {
node = sym->parent->parent->child[0];
if (node && node->info == 1) {
sym->parent->info = 0;
node->info = 0;
sym->parent->parent->info = 1;
sym = sym->parent->parent;
} else {
if (sym == sym->parent->child[0]) {
sym = sym->parent;
zend_syms_rotateright(sym);
}
sym->parent->info = 0;
sym->parent->parent->info = 1;
zend_syms_rotateleft(sym->parent->parent);
}
}
}
} else {
symbols = sym;
}
symbols->info = 0;
}
static void zend_jit_disasm_destroy_symbols(zend_sym_node *n) {
if (n) {
if (n->child[0]) {
zend_jit_disasm_destroy_symbols(n->child[0]);
} else if (n->child[1]) {
zend_jit_disasm_destroy_symbols(n->child[1]);
}
free(n);
}
}
static const char* zend_jit_disasm_find_symbol(uint64_t addr,
int64_t *offset) {
zend_sym_node *node = symbols;
while (node) {
if (addr < node->addr) {
node = node->child[0];
} else if (addr > node->end) {
node = node->child[1];
} else {
*offset = addr - node->addr;
return node->name;
}
}
return NULL;
}
static const char* zend_jit_disasm_resolver(struct ud *ud,
uint64_t addr,
int64_t *offset)
{
#ifndef _WIN32
((void)ud);
const char *name;
void *a = (void*)(zend_uintptr_t)(addr);
Dl_info info;
name = zend_jit_disasm_find_symbol(addr, offset);
if (name) {
return name;
}
if (dladdr(a, &info)
&& info.dli_sname != NULL
&& info.dli_saddr == a) {
return info.dli_sname;
}
#else
const char *name;
name = zend_jit_disasm_find_symbol(addr, offset);
if (name) {
return name;
}
#endif
return NULL;
}
static int zend_jit_cmp_labels(Bucket *b1, Bucket *b2)
{
return ((b1->h > b2->h) > 0) ? 1 : -1;
}
static int zend_jit_disasm(const char *name,
const char *filename,
const zend_op_array *op_array,
zend_cfg *cfg,
const void *start,
size_t size)
{
const void *end = (void *)((char *)start + size);
zval zv, *z;
zend_long n, m;
HashTable labels;
const struct ud_operand *op;
uint64_t addr;
int b;
if (name) {
fprintf(stderr, "%s: ; (%s)\n", name, filename ? filename : "unknown");
}
ud_set_input_buffer(&ud, (uint8_t*)start, (uint8_t*)end - (uint8_t*)start);
ud_set_pc(&ud, (uint64_t)(uintptr_t)start);
zend_hash_init(&labels, 8, NULL, NULL, 0);
if (op_array && cfg) {
ZVAL_FALSE(&zv);
for (b = 0; b < cfg->blocks_count; b++) {
if (cfg->blocks[b].flags & (ZEND_BB_ENTRY|ZEND_BB_RECV_ENTRY)) {
addr = (uint64_t)(uintptr_t)op_array->opcodes[cfg->blocks[b].start].handler;
if (addr >= (uint64_t)(uintptr_t)start && addr < (uint64_t)(uintptr_t)end) {
zend_hash_index_add(&labels, addr, &zv);
}
}
}
}
ZVAL_TRUE(&zv);
while (ud_disassemble(&ud)) {
op = ud_insn_opr(&ud, 0);
if (op && op->type == UD_OP_JIMM) {
addr = ud_syn_rel_target(&ud, (struct ud_operand*)op);
if (addr >= (uint64_t)(uintptr_t)start && addr < (uint64_t)(uintptr_t)end) {
zend_hash_index_add(&labels, addr, &zv);
}
}
}
zend_hash_sort(&labels, (compare_func_t)zend_jit_cmp_labels, 0);
/* label numbering */
n = 0; m = 0;
ZEND_HASH_FOREACH_VAL(&labels, z) {
if (Z_TYPE_P(z) == IS_FALSE) {
m--;
ZVAL_LONG(z, m);
} else {
n++;
ZVAL_LONG(z, n);
}
} ZEND_HASH_FOREACH_END();
ud_set_input_buffer(&ud, (uint8_t*)start, (uint8_t*)end - (uint8_t*)start);
ud_set_pc(&ud, (uint64_t)(uintptr_t)start);
while (ud_disassemble(&ud)) {
addr = ud_insn_off(&ud);
z = zend_hash_index_find(&labels, addr);
if (z) {
if (Z_LVAL_P(z) < 0) {
fprintf(stderr, ".ENTRY" ZEND_LONG_FMT ":\n", -Z_LVAL_P(z));
} else {
fprintf(stderr, ".L" ZEND_LONG_FMT ":\n", Z_LVAL_P(z));
}
}
op = ud_insn_opr(&ud, 0);
if (op && op->type == UD_OP_JIMM) {
addr = ud_syn_rel_target(&ud, (struct ud_operand*)op);
if (addr >= (uint64_t)(uintptr_t)start && addr < (uint64_t)(uintptr_t)end) {
z = zend_hash_index_find(&labels, addr);
if (z) {
const char *str = ud_insn_asm(&ud);
int len;
len = 0;
while (str[len] != 0 && str[len] != ' ' && str[len] != '\t') {
len++;
}
if (str[len] != 0) {
while (str[len] == ' ' || str[len] == '\t') {
len++;
}
if (Z_LVAL_P(z) < 0) {
fprintf(stderr, "\t%.*s.ENTRY" ZEND_LONG_FMT "\n", len, str, -Z_LVAL_P(z));
} else {
fprintf(stderr, "\t%.*s.L" ZEND_LONG_FMT "\n", len, str, Z_LVAL_P(z));
}
continue;
}
}
}
}
fprintf(stderr, "\t%s\n", ud_insn_asm(&ud));
}
fprintf(stderr, "\n");
zend_hash_destroy(&labels);
return 1;
}
static int zend_jit_disasm_init(void)
{
ud_init(&ud);
#if defined(__x86_64__) || defined(_WIN64)
ud_set_mode(&ud, 64);
#else
ud_set_mode(&ud, 32);
#endif
#if DISASM_INTEL_SYNTAX
ud_set_syntax(&ud, UD_SYN_INTEL);
#else
ud_set_syntax(&ud, UD_SYN_ATT);
#endif
ud_set_sym_resolver(&ud, zend_jit_disasm_resolver);
#ifndef ZTS
#define REGISTER_EG(n) \
zend_jit_disasm_add_symbol("EG("#n")", \
(uint64_t)(uintptr_t)&executor_globals.n, sizeof(executor_globals.n))
REGISTER_EG(uninitialized_zval);
REGISTER_EG(exception);
REGISTER_EG(vm_interrupt);
REGISTER_EG(exception_op);
REGISTER_EG(timed_out);
REGISTER_EG(current_execute_data);
REGISTER_EG(vm_stack_top);
REGISTER_EG(vm_stack_end);
REGISTER_EG(symbol_table);
#undef REGISTER_EG
#endif
/* Register JIT helper functions */
#define REGISTER_HELPER(n) \
zend_jit_disasm_add_symbol(#n, \
(uint64_t)(uintptr_t)n, sizeof(void*));
REGISTER_HELPER(memcmp);
REGISTER_HELPER(zend_jit_init_func_run_time_cache_helper);
REGISTER_HELPER(zend_jit_find_func_helper);
REGISTER_HELPER(zend_jit_extend_stack_helper);
REGISTER_HELPER(zend_jit_int_extend_stack_helper);
REGISTER_HELPER(zend_jit_leave_nested_func_helper);
REGISTER_HELPER(zend_jit_leave_top_func_helper);
REGISTER_HELPER(zend_jit_symtable_find);
REGISTER_HELPER(zend_jit_hash_index_lookup_rw);
REGISTER_HELPER(zend_jit_hash_index_lookup_w);
REGISTER_HELPER(zend_jit_hash_lookup_rw);
REGISTER_HELPER(zend_jit_hash_lookup_w);
REGISTER_HELPER(zend_jit_symtable_lookup_rw);
REGISTER_HELPER(zend_jit_symtable_lookup_w);
REGISTER_HELPER(zend_jit_fetch_dimension_rw_long_helper);
REGISTER_HELPER(zend_jit_undefined_op_helper);
REGISTER_HELPER(zend_jit_fetch_dim_r_helper);
REGISTER_HELPER(zend_jit_fetch_dim_is_helper);
REGISTER_HELPER(zend_jit_fetch_dim_isset_helper);
REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper);
REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper);
REGISTER_HELPER(zend_jit_fetch_dim_obj_r_helper);
REGISTER_HELPER(zend_jit_fetch_dim_obj_is_helper);
REGISTER_HELPER(zend_jit_fetch_dim_rw_helper);
REGISTER_HELPER(zend_jit_fetch_dim_w_helper);
REGISTER_HELPER(zend_jit_assign_dim_helper);
REGISTER_HELPER(zend_jit_assign_dim_op_helper);
REGISTER_HELPER(zend_jit_fast_assign_concat_helper);
REGISTER_HELPER(zend_jit_fast_concat_helper);
REGISTER_HELPER(zend_jit_isset_dim_helper);
REGISTER_HELPER(zend_jit_free_call_frame);
REGISTER_HELPER(zend_jit_zval_copy_deref_helper)
REGISTER_HELPER(zend_jit_new_ref_helper);
REGISTER_HELPER(zend_jit_fetch_global_helper);
REGISTER_HELPER(zend_jit_verify_arg_object);
REGISTER_HELPER(zend_jit_verify_arg_slow);
REGISTER_HELPER(zend_jit_fetch_obj_r_slow);
REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic);
REGISTER_HELPER(zend_jit_fetch_obj_is_slow);
REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic);
REGISTER_HELPER(zend_jit_vm_stack_free_args_helper);
REGISTER_HELPER(zend_jit_copy_extra_args_helper);
REGISTER_HELPER(zend_jit_deprecated_helper);
REGISTER_HELPER(zend_jit_assign_const_to_typed_ref);
REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref);
REGISTER_HELPER(zend_jit_assign_var_to_typed_ref);
REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref);
REGISTER_HELPER(zend_jit_pre_inc_typed_ref);
REGISTER_HELPER(zend_jit_pre_dec_typed_ref);
REGISTER_HELPER(zend_jit_post_inc_typed_ref);
REGISTER_HELPER(zend_jit_post_dec_typed_ref);
REGISTER_HELPER(zend_jit_assign_op_to_typed_ref);
REGISTER_HELPER(zend_jit_only_vars_by_reference);
REGISTER_HELPER(zend_jit_invalid_array_access);
REGISTER_HELPER(zend_jit_prepare_assign_dim_ref);
REGISTER_HELPER(zend_runtime_jit);
REGISTER_HELPER(zend_jit_hot_func);
#undef REGISTER_HELPER
#ifndef _WIN32
zend_elf_load_symbols();
#endif
if (zend_vm_kind() == ZEND_VM_KIND_HYBRID) {
zend_op opline;
memset(&opline, 0, sizeof(opline));
opline.opcode = ZEND_DO_UCALL;
opline.result_type = IS_UNUSED;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_DO_UCALL;
opline.result_type = IS_VAR;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_DO_FCALL_BY_NAME;
opline.result_type = IS_UNUSED;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_DO_FCALL_BY_NAME;
opline.result_type = IS_VAR;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_DO_FCALL;
opline.result_type = IS_UNUSED;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_DO_FCALL;
opline.result_type = IS_VAR;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_RETURN;
opline.op1_type = IS_CONST;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_RETURN_SPEC_CONST_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_RETURN;
opline.op1_type = IS_TMP_VAR;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_RETURN_SPEC_TMP_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_RETURN;
opline.op1_type = IS_VAR;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_RETURN_SPEC_VAR_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
opline.opcode = ZEND_RETURN;
opline.op1_type = IS_CV;
zend_vm_set_opcode_handler(&opline);
zend_jit_disasm_add_symbol("ZEND_RETURN_SPEC_CV_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
}
return 1;
}
static void zend_jit_disasm_shutdown(void)
{
zend_jit_disasm_destroy_symbols(symbols);
}
|