/
bytekit.c
1955 lines (1685 loc) · 67.9 KB
/
bytekit.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Bytekit |
+----------------------------------------------------------------------+
| Copyright (c) 2008-2009 SektionEins GmbH |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Bytekit license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.bytekit.org/license.txt |
| If you did not receive a copy of the Bytekit license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| license@bytekit.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Stefan Esser <stefan.esser@sektioneins.de> |
+----------------------------------------------------------------------+
*/
/*************************************************************************/
/* This code combines features of vld, parsekit with additional features */
/* therefore some parts of thr code are based on these extensions */
/*************************************************************************/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "php_bytekit.h"
/* This code does only work on PHP >= 5.2.0 - PHP 6 not yet supported */
#if (!defined(PHP_VERSION_ID) || (PHP_VERSION_ID < 50200) || (PHP_VERSION_ID >= 60000))
# error "Bytekit can only be compiled for PHP >= 5.2.0 and not yet for PHP 6"
#endif
ZEND_DECLARE_MODULE_GLOBALS(bytekit)
/* original error callback handler */
void (*bytekit_original_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
/* {{{ bytekit_error_cb
ignore, catch and return non core errors */
static void bytekit_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
{
char *buffer;
int buffer_len;
zval *tmpzval;
TSRMLS_FETCH();
/* Do not touch core errors and avoid loops */
if (type == E_CORE_ERROR || BYTEKIT_G(in_error_cb)) {
bytekit_original_error_cb(type, (char *)error_filename, error_lineno, format, args);
return;
}
/* Ignore errors if selected */
if (!BYTEKIT_G(compile_errors)) {
return;
}
/* We are processing an error */
BYTEKIT_G(in_error_cb) = 1;
MAKE_STD_ZVAL(tmpzval);
array_init(tmpzval);
add_assoc_long(tmpzval, "errno", type);
add_assoc_string(tmpzval, "filename", (char *)error_filename, 1);
add_assoc_long(tmpzval, "lineno", error_lineno);
buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
add_assoc_stringl(tmpzval, "errstr", buffer, buffer_len, 1);
if (Z_TYPE_P(BYTEKIT_G(compile_errors)) == IS_NULL) {
array_init(BYTEKIT_G(compile_errors));
}
add_next_index_zval(BYTEKIT_G(compile_errors), tmpzval);
/* We are no longer processing the error */
BYTEKIT_G(in_error_cb) = 0;
}
/* }}} */
/* {{{ bytekit_get_opcode_info */
static char* bytekit_get_opcode_info(long val, long *pflags, long *pflags2)
{
bytekit_define_list *names;
for (names = bytekit_opcode_names; names->str; names++) {
if (names->val == val) {
if (pflags) {
*pflags = names->flags;
}
if (pflags2) {
*pflags2 = names->flags2;
}
return names->str;
}
}
return BYTEKIT_UNKNOWN;
}
/* }}} */
/* {{{ bytekit_get_opcode_mnemonic */
static char* bytekit_get_opcode_mnemonic(zend_op_array *zop, zend_op *op TSRMLS_DC)
{
bytekit_define_list *names;
char *postfix = "", *n, *r;
int i;
switch (op->opcode) {
case ZEND_INCLUDE_OR_EVAL:
switch (Z_LVAL(op->op2.u.constant)) {
case ZEND_INCLUDE:
return estrndup("INCLUDE", sizeof("INCLUDE")-1);
case ZEND_REQUIRE:
return estrndup("REQUIRE", sizeof("REQUIRE")-1);
case ZEND_INCLUDE_ONCE:
return estrndup("INCLUDE_ONCE", sizeof("INCLUDE_ONCE")-1);
case ZEND_REQUIRE_ONCE:
return estrndup("REQUIRE_ONCE", sizeof("REQUIRE_ONCE")-1);
case ZEND_EVAL:
return estrndup("EVAL", sizeof("EVAL")-1);
default:
/* UNKNOWN decode as INCLUDE_OR_EVAL */
break;
}
break;
case ZEND_ISSET_ISEMPTY_VAR:
{
int ev = op->extended_value & ZEND_ISSET_ISEMPTY_MASK;
if (ev == ZEND_ISSET) return estrndup("ISSET_VAR", sizeof("ISSET_VAR")-1);
if (ev == ZEND_ISEMPTY) return estrndup("ISEMPTY_VAR", sizeof("ISEMPTY_VAR")-1);
}
break;
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
{
int ev = op->extended_value & ZEND_ISSET_ISEMPTY_MASK;
if (ev == ZEND_ISSET) return estrndup("ISSET_DIM_OBJ", sizeof("ISSET_DIM_OBJ")-1);
if (ev == ZEND_ISEMPTY) return estrndup("ISEMPTY_DIM_OBJ", sizeof("ISEMPTY_DIM_OBJ")-1);
}
break;
case ZEND_ISSET_ISEMPTY_PROP_OBJ:
{
int ev = op->extended_value & ZEND_ISSET_ISEMPTY_MASK;
if (ev == ZEND_ISSET) return estrndup("ISSET_PROP_OBJ", sizeof("ISSET_PROP_OBJ")-1);
if (ev == ZEND_ISEMPTY) return estrndup("ISEMPTY_PROP_OBJ", sizeof("ISEMPTY_PROP_OBJ")-1);
}
break;
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
case ZEND_ASSIGN_CONCAT:
case ZEND_ASSIGN_BW_OR:
case ZEND_ASSIGN_BW_AND:
case ZEND_ASSIGN_BW_XOR:
if (op->extended_value == ZEND_ASSIGN_OBJ) {
postfix = "_OBJ";
} else if (op->extended_value == ZEND_ASSIGN_DIM) {
postfix = "_DIM";
}
n = bytekit_get_opcode_info(op->opcode, NULL, NULL);
if (n == BYTEKIT_UNKNOWN) {
/* should not happen */
n = "UNKNOWN";
}
i = strlen(n);
r = emalloc(i + strlen(postfix) + 1);
memcpy(r, n, i);
memcpy(r + i, postfix, strlen(postfix));
r[i+strlen(postfix)] = 0;
return r;
case ZEND_CAST:
switch (op->extended_value) {
case IS_NULL:
return estrndup("CAST_NULL", sizeof("CAST_NULL")-1);
case IS_BOOL:
return estrndup("CAST_BOOL", sizeof("CAST_BOOL")-1);
case IS_LONG:
return estrndup("CAST_LONG", sizeof("CAST_LONG")-1);
case IS_DOUBLE:
return estrndup("CAST_DOUBLE", sizeof("CAST_DOUBLE")-1);
case IS_STRING:
return estrndup("CAST_STRING", sizeof("CAST_STRING")-1);
case IS_ARRAY:
return estrndup("CAST_ARRAY", sizeof("CAST_ARRAY")-1);
case IS_OBJECT:
return estrndup("CAST_OBJECT", sizeof("CAST_OBJECT")-1);
default:
/* UNKNOWN decode as CAST */
break;
}
break;
default:
break;
}
return estrdup(bytekit_get_opcode_info(op->opcode, NULL, NULL));
}
/* }}} */
/* {{{ bytekit_get_opcode_flags */
static int bytekit_get_opcode_flags(zend_op_array *zop, zend_op *op, long *flags, long *flags2 TSRMLS_DC)
{
bytekit_define_list *names;
switch (op->opcode) {
case ZEND_INCLUDE_OR_EVAL:
switch (Z_LVAL(op->op2.u.constant)) {
case ZEND_INCLUDE:
case ZEND_REQUIRE:
case ZEND_INCLUDE_ONCE:
case ZEND_REQUIRE_ONCE:
case ZEND_EVAL:
break;
default:
/* UNKNOWN decode as INCLUDE_OR_EVAL */
break;
}
break;
case ZEND_CAST:
switch (op->extended_value) {
case IS_NULL:
case IS_BOOL:
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
case IS_ARRAY:
case IS_OBJECT:
break;
default:
/* UNKNOWN decode as CAST */
break;
}
break;
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
case ZEND_ASSIGN_CONCAT:
case ZEND_ASSIGN_BW_OR:
case ZEND_ASSIGN_BW_AND:
case ZEND_ASSIGN_BW_XOR:
bytekit_get_opcode_info(op->opcode, flags, flags2);
if (op->extended_value == ZEND_ASSIGN_OBJ || op->extended_value == ZEND_ASSIGN_DIM) {
*flags2 = BYTEKIT_OP1_CVAR | BYTEKIT_OP2_CVAR;
}
return SUCCESS;
case ZEND_FE_FETCH:
if (op->extended_value & ZEND_FE_FETCH_WITH_KEY) {
*flags = BYTEKIT_RES_WRITE | BYTEKIT_RES_CVAR | BYTEKIT_OP1_CVAR | BYTEKIT_OP2_OPNUM | BYTEKIT_EXT_SPECIAL;
*flags2 = BYTEKIT_RES_WRITE | BYTEKIT_RES_CVAR;
return SUCCESS;
}
*flags = BYTEKIT_RES_WRITE | BYTEKIT_RES_CVAR | BYTEKIT_OP1_CVAR | BYTEKIT_OP2_OPNUM | BYTEKIT_EXT_SPECIAL;
*flags2 = 0;
return SUCCESS;
default:
break;
}
*flags = *flags2 = 0;
if (bytekit_get_opcode_info(op->opcode, flags, flags2) == BYTEKIT_UNKNOWN) {
return SUCCESS;
}
return FAILURE;
}
/* }}} */
/* {{{ bytekit_get_opcode_oplines */
static int bytekit_get_opcode_oplines(zend_op_array *zop, zend_op *op TSRMLS_DC)
{
int length = 1;
switch (op->opcode) {
case ZEND_ASSIGN_OBJ:
case ZEND_ASSIGN_DIM:
length = 2;
break;
case ZEND_INIT_NS_FCALL_BY_NAME:
length = 2;
break;
case ZEND_FE_FETCH:
length = 2;
break;
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
case ZEND_ASSIGN_CONCAT:
case ZEND_ASSIGN_BW_OR:
case ZEND_ASSIGN_BW_AND:
case ZEND_ASSIGN_BW_XOR:
if (op->extended_value == ZEND_ASSIGN_OBJ || op->extended_value == ZEND_ASSIGN_DIM) {
length = 2;
}
}
if (length > 1) {
if ((op+1)->opcode != ZEND_OP_DATA) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "bytekit_get_opcode_oplines: ZEND_OP_DATA expected but %u found.", (op+1)->opcode);
}
}
return length;
}
/* }}} */
/* {{{ bytekit_destroy_function */
static int bytekit_destroy_function(HashTable *function_table, zend_function *function, char *function_name, int function_name_len TSRMLS_DC)
{
/* TODO: properly destroy the function */
if (zend_hash_del(function_table, function_name, function_name_len) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ bytekit_cleanup_functions */
static int bytekit_cleanup_functions(HashTable *function_table, int clean_count TSRMLS_DC)
{
HashPosition pos;
zend_hash_internal_pointer_end_ex(function_table, &pos);
while (clean_count < zend_hash_num_elements(function_table)) {
long func_index;
unsigned int func_name_len;
char *func_name;
zend_function *function;
if (zend_hash_get_current_data_ex(function_table, (void **)&function, &pos) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_functions: error traversing function table.");
return FAILURE;
}
if (function->type == ZEND_INTERNAL_FUNCTION) {
/* Inherited internal method */
zend_hash_move_backwards_ex(function_table, &pos);
clean_count++;
continue;
} else if (function->type != ZEND_USER_FUNCTION) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_functions: illegal function entry found.");
return FAILURE;
}
if (zend_hash_get_current_key_ex(function_table, &func_name, &func_name_len, &func_index, 0, &pos) == HASH_KEY_IS_STRING) {
zend_hash_move_backwards_ex(function_table, &pos);
if (bytekit_destroy_function(function_table, function, func_name, func_name_len TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_functions: unable to destroy function.");
return FAILURE;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_functions: illegal function table key found.");
return FAILURE;
}
}
return SUCCESS;
}
/* }}} */
/* {{{ bytekit_destroy_class */
static int bytekit_destroy_class(HashTable *class_table, zend_class_entry *class_entry, char *class_name, int class_name_len TSRMLS_DC)
{
int rval = SUCCESS;
/* TODO: properly destroy the class */
if (zend_hash_num_elements(&(class_entry->function_table)) > 0) {
rval = bytekit_cleanup_functions(&(class_entry->function_table), 0 TSRMLS_CC);
}
if (zend_hash_del(class_table, class_name, class_name_len) == FAILURE) {
rval = FAILURE;
}
return rval;
}
/* }}} */
/* {{{ bytekit_cleanup_classes */
static int bytekit_cleanup_classes(HashTable *class_table, int clean_count TSRMLS_DC)
{
while (clean_count < zend_hash_num_elements(class_table)) {
long class_index;
unsigned int class_name_len;
char *class_name;
zend_class_entry *class_entry, **pce;
zend_hash_internal_pointer_end(class_table);
if (zend_hash_get_current_data(class_table, (void **)&pce) == FAILURE || !pce || !(*pce)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_classes: unable to traverse class table.");
return FAILURE;
}
class_entry = *pce;
if (class_entry->type != ZEND_USER_CLASS) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_classes: illegal class found in class table.");
return FAILURE;
}
if (zend_hash_get_current_key_ex(class_table, &class_name, &class_name_len, &class_index, 0, NULL) == HASH_KEY_IS_STRING) {
if (bytekit_destroy_class(class_table, class_entry, class_name, class_name_len TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_classes: unable to destroy class.");
return FAILURE;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bytekit_cleanup_classes: illegal class table key found.");
}
}
return SUCCESS;
}
/* }}} */
/* {{{ bytekit_cleanup_functions_and_classes */
static void bytekit_cleanup_functions_and_classes(int original_num_functions, int original_num_classes TSRMLS_DC)
{
if (original_num_functions < zend_hash_num_elements(EG(function_table))) {
bytekit_cleanup_functions(EG(function_table), original_num_functions TSRMLS_CC);
}
if (original_num_classes < zend_hash_num_elements(EG(class_table))) {
bytekit_cleanup_classes(EG(class_table), original_num_classes TSRMLS_CC);
}
}
/* }}} */
inline int bytekit_disassemble_zval_null(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
add_assoc_string(return_value, "string", "null", 1);
return SUCCESS;
}
inline int bytekit_disassemble_zval_long(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "%ld", value.lval);
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_double(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "%g", value.dval);
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_string(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
char *newstr;
if (value.str.len == 0) {
newstr = estrdup("");
} else {
newstr = php_addcslashes(value.str.val, value.str.len, NULL, 0, "\n\r\t\a\b\f\v\0", 9 TSRMLS_CC);
}
spprintf(&tmpstr, 0, "'%s'", newstr);
efree(newstr);
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_array(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "<array>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_object(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "<object>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_bool(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, value.lval ? "true" : "false");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_resource(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "<resource>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_constant(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "<const>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
inline int bytekit_disassemble_zval_constant_array(zval *return_value, zvalue_value value TSRMLS_DC)
{
char *tmpstr;
spprintf(&tmpstr, 0, "<const_array>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
int bytekit_disassemble_zval (zval *return_value, zval *val TSRMLS_DC)
{
char *tmpstr;
zval *cval;
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VALUE);
MAKE_STD_ZVAL(cval);
*cval = *val;
zval_copy_ctor(cval);
#if PHP_VERSION_ID >= 50300
Z_SET_REFCOUNT_P(cval, 1);
Z_UNSET_ISREF_P(cval);
#else
cval->refcount = 1;
cval->is_ref = 0;
#endif
add_assoc_zval(return_value, "value", cval);
switch (val->type) {
case IS_NULL: return bytekit_disassemble_zval_null (return_value, val->value TSRMLS_CC);
case IS_LONG: return bytekit_disassemble_zval_long (return_value, val->value TSRMLS_CC);
case IS_DOUBLE: return bytekit_disassemble_zval_double (return_value, val->value TSRMLS_CC);
case IS_STRING: return bytekit_disassemble_zval_string (return_value, val->value TSRMLS_CC);
case IS_ARRAY: return bytekit_disassemble_zval_array (return_value, val->value TSRMLS_CC);
case IS_OBJECT: return bytekit_disassemble_zval_object (return_value, val->value TSRMLS_CC);
case IS_BOOL: return bytekit_disassemble_zval_bool (return_value, val->value TSRMLS_CC);
case IS_RESOURCE: return bytekit_disassemble_zval_resource (return_value, val->value TSRMLS_CC);
case IS_CONSTANT: return bytekit_disassemble_zval_constant (return_value, val->value TSRMLS_CC);
case IS_CONSTANT_ARRAY: return bytekit_disassemble_zval_constant_array (return_value, val->value TSRMLS_CC);
}
spprintf(&tmpstr, 0, "<unknown>");
add_assoc_string(return_value, "string", tmpstr, 0);
return SUCCESS;
}
/* {{{ bytekit_address2string */
static char *bytekit_address2string(long long address, int cnt TSRMLS_DC)
{
char *res = (char *)emalloc(64);
if (cnt == 0) {
sprintf(res, "%lld", address);
} else {
sprintf(res, "%0*lld", cnt, address);
}
return res;
}
/* }}} */
/* {{{ bytekit_address2hexstring */
static char *bytekit_address2hexstring(long long address, int cnt TSRMLS_DC)
{
char *res = (char *)emalloc(64);
if (cnt == 0) {
sprintf(res, "%llx", address);
} else {
sprintf(res, "%0*llx", cnt, address);
}
return res;
}
/* }}} */
/* {{{ bytekit_disassemble_node */
static int bytekit_disassemble_node(zval *return_value, zend_op_array *op_array, znode *node, int i, long long address, long opcode_flags, long options TSRMLS_DC)
{
char *tmpstr;
char *base_address = BYTEKIT_G(opcodes_base) ? BYTEKIT_G(opcodes_base) : op_array->opcodes;
long long dst;
char *dststr;
array_init(return_value);
switch (opcode_flags) {
case BYTEKIT_OP_OPNUM:
dst = address - i + node->u.opline_num;
dststr = bytekit_address2hexstring(dst, 0 TSRMLS_CC);
spprintf(&tmpstr, 0, "loc_%s", dststr);
efree(dststr);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_SYMBOL);
add_assoc_long(return_value, "value", dst);
return SUCCESS;
case BYTEKIT_OP_NUM:
spprintf(&tmpstr, 0, "%d", node->u.opline_num);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VALUE);
add_assoc_long(return_value, "value", node->u.opline_num );
return SUCCESS;
case BYTEKIT_OP_JMPADDR:
dst = address - i + ((char *)node->u.jmp_addr - base_address) / sizeof(zend_op);
dststr = bytekit_address2hexstring(dst, 0 TSRMLS_CC);
spprintf(&tmpstr, 0, "loc_%s", dststr);
efree(dststr);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_SYMBOL);
add_assoc_long(return_value, "value", dst);
return SUCCESS;
case BYTEKIT_OP_TVAR:
spprintf(&tmpstr, 0, "~%d", node->u.var / sizeof(temp_variable));
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_TMP_VAR);
add_assoc_long(return_value, "value", node->u.var / sizeof(temp_variable));
return SUCCESS;
}
switch (node->op_type) {
case IS_CONST: /* 1 */
bytekit_disassemble_zval (return_value, &node->u.constant TSRMLS_CC);
return SUCCESS;
case IS_CV: /* 16 */
spprintf(&tmpstr, 0, "!%d", node->u.var);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_CV);
add_assoc_long(return_value, "value", node->u.var );
return SUCCESS;
case IS_TMP_VAR: /* 2 */
spprintf(&tmpstr, 0, "~%d", node->u.var / sizeof(temp_variable));
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_TMP_VAR);
add_assoc_long(return_value, "value", node->u.var / sizeof(temp_variable));
return SUCCESS;
case IS_VAR: /* 4 */
spprintf(&tmpstr, 0, "$%d", node->u.var / sizeof(temp_variable));
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VAR);
add_assoc_long(return_value, "value", node->u.var / sizeof(temp_variable));
return SUCCESS;
case IS_UNUSED:
return FAILURE;
default:
return SUCCESS;
}
}
/* }}} */
/* {{{ bytekit_disassemble_ext */
static int bytekit_disassemble_ext(zval *return_value, zend_op_array *op_array, ulong ext, int i, long long address, long opcode_flags, long options TSRMLS_DC)
{
char *tmpstr;
char *base_address = BYTEKIT_G(opcodes_base) ? BYTEKIT_G(opcodes_base) : op_array->opcodes;
long long dst;
char *dststr;
array_init(return_value);
switch (opcode_flags) {
case BYTEKIT_EXT_SPECIAL:
if ((options & BYTEKIT_SHOW_SPECIALS) == 0) {
return FAILURE;
}
spprintf(&tmpstr, 0, "s(%d)", ext);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VALUE);
add_assoc_long(return_value, "value", ext);
return SUCCESS;
case BYTEKIT_EXT_NUM:
spprintf(&tmpstr, 0, "%d", ext);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VALUE);
add_assoc_long(return_value, "value", ext);
return SUCCESS;
case BYTEKIT_EXT_FETCH:
spprintf(&tmpstr, 0, "f(%d)", ext);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_VALUE);
add_assoc_long(return_value, "value", ext);
return SUCCESS;
case BYTEKIT_EXT_OPNUM:
dst = address - i + ext;
dststr = bytekit_address2hexstring(dst, 0 TSRMLS_CC);
spprintf(&tmpstr, 0, "loc_%s", dststr);
efree(dststr);
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_SYMBOL);
add_assoc_long(return_value, "value", dst);
return SUCCESS;
case BYTEKIT_EXT_TVAR:
spprintf(&tmpstr, 0, "~%d", ext / sizeof(temp_variable));
add_assoc_string(return_value, "string", tmpstr, 0);
add_assoc_long(return_value, "type", BYTEKIT_TYPE_TMP_VAR);
add_assoc_long(return_value, "value", ext / sizeof(temp_variable));
return SUCCESS;
}
return FAILURE;
}
/* }}} */
/* {{{ bytekit_disassemble_op */
static void bytekit_disassemble_op(zval *return_value, zend_op_array *op_array, zend_op *op, int i, long long address, long options, int *oplines TSRMLS_DC)
{
zval *result, *op1, *op2, *operands, *ext;
long opcode_flags = 0, opcode_flags2 = 0;
int opcode;
char *mnemonic;
array_init(return_value);
opcode = op->opcode;
add_assoc_string(return_value, "address", bytekit_address2string(address, 0 TSRMLS_CC), 0);
add_assoc_long(return_value, "opline", i);
/* determine mnemonic (including our aliases) and further opcode specific infos */
mnemonic = bytekit_get_opcode_mnemonic(op_array, op TSRMLS_CC);
add_assoc_string(return_value, "mnemonic", mnemonic, 0);
bytekit_get_opcode_flags(op_array, op, &opcode_flags, &opcode_flags2 TSRMLS_CC);
if (oplines) {
*oplines = bytekit_get_opcode_oplines(op_array, op TSRMLS_CC);
}
/* initialize operands */
MAKE_STD_ZVAL(operands);
array_init(operands);
/* args: result, op1, op2 */
if ((opcode_flags & BYTEKIT_RES_USED) && ((opcode_flags & BYTEKIT_RES_HIDE)==0) && (!(op->result.u.EA.type & EXT_TYPE_UNUSED) || (opcode_flags & BYTEKIT_RES_FORCE))) {
MAKE_STD_ZVAL(result);
if (bytekit_disassemble_node(result, op_array, &(op->result), i, address, (opcode_flags & BYTEKIT_RES_TYPE_MASK) >> BYTEKIT_RES_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(result, "flags", BYTEKIT_SRC_RES1 | ((opcode_flags & BYTEKIT_RES_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, result);
} else {
zval_dtor(result);
FREE_ZVAL(result);
}
}
if ((opcode_flags2 & BYTEKIT_RES_USED) && ((opcode_flags2 & BYTEKIT_RES_HIDE)==0) && !((op+1)->result.u.EA.type & EXT_TYPE_UNUSED)) {
MAKE_STD_ZVAL(result);
if (bytekit_disassemble_node(result, op_array, &((op+1)->result), i, address, (opcode_flags2 & BYTEKIT_RES_TYPE_MASK) >> BYTEKIT_RES_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(result, "flags", BYTEKIT_SRC_RES2 | ((opcode_flags2 & BYTEKIT_RES_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, result);
} else {
zval_dtor(result);
FREE_ZVAL(result);
}
}
if ((opcode_flags & BYTEKIT_OP1_USED) && ((opcode_flags & BYTEKIT_OP1_HIDE)==0)) {
MAKE_STD_ZVAL(op1);
if (bytekit_disassemble_node(op1, op_array, &(op->op1), i, address, (opcode_flags & BYTEKIT_OP1_TYPE_MASK) >> BYTEKIT_OP1_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(op1, "flags", BYTEKIT_SRC_OP1 | ((opcode_flags & BYTEKIT_OP1_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, op1);
} else {
zval_dtor(op1);
FREE_ZVAL(op1);
}
}
if ((opcode_flags & BYTEKIT_OP2_USED) && ((opcode_flags & BYTEKIT_OP2_HIDE)==0)) {
MAKE_STD_ZVAL(op2);
if (bytekit_disassemble_node(op2, op_array, &(op->op2), i, address, (opcode_flags & BYTEKIT_OP2_TYPE_MASK) >> BYTEKIT_OP2_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(op2, "flags", BYTEKIT_SRC_OP2 | ((opcode_flags & BYTEKIT_OP2_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, op2);
} else {
zval_dtor(op2);
FREE_ZVAL(op2);
}
}
if ((opcode_flags & BYTEKIT_EXT_USED) && ((opcode_flags & BYTEKIT_EXT_HIDE)==0)) {
MAKE_STD_ZVAL(ext);
if (bytekit_disassemble_ext(ext, op_array, op->extended_value, i, address, opcode_flags & BYTEKIT_EXT_TYPE_MASK, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(ext, "flags", BYTEKIT_SRC_EXT1);
add_next_index_zval(operands, ext);
} else {
zval_dtor(ext);
FREE_ZVAL(ext);
}
}
if ((opcode_flags2 & BYTEKIT_OP1_USED) && ((opcode_flags2 & BYTEKIT_OP1_HIDE)==0)) {
MAKE_STD_ZVAL(op1);
if (bytekit_disassemble_node(op1, op_array, &((op+1)->op1), i, address, (opcode_flags2 & BYTEKIT_OP1_TYPE_MASK) >> BYTEKIT_OP1_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(op1, "flags", BYTEKIT_SRC_OP3 | ((opcode_flags2 & BYTEKIT_OP1_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, op1);
} else {
zval_dtor(op1);
FREE_ZVAL(op1);
}
}
if ((opcode_flags2 & BYTEKIT_OP2_USED) && ((opcode_flags2 & BYTEKIT_OP2_HIDE)==0)) {
MAKE_STD_ZVAL(op2);
if (bytekit_disassemble_node(op2, op_array, &((op+1)->op2), i, address, (opcode_flags2 & BYTEKIT_OP2_TYPE_MASK) >> BYTEKIT_OP2_SHIFT, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(op2, "flags", BYTEKIT_SRC_OP4 | ((opcode_flags & BYTEKIT_OP2_WRITE) ? BYTEKIT_OPERAND_WRITTEN : 0));
add_next_index_zval(operands, op2);
} else {
zval_dtor(op2);
FREE_ZVAL(op2);
}
}
if ((opcode_flags2 & BYTEKIT_EXT_USED) && ((opcode_flags2 & BYTEKIT_EXT_HIDE)==0)) {
MAKE_STD_ZVAL(ext);
if (bytekit_disassemble_ext(ext, op_array, (op+1)->extended_value, i, address, opcode_flags2 & BYTEKIT_EXT_TYPE_MASK, options TSRMLS_CC) == SUCCESS) {
add_assoc_long(ext, "flags", BYTEKIT_SRC_EXT2);
add_next_index_zval(operands, ext);
} else {
zval_dtor(ext);
FREE_ZVAL(ext);
}
}
add_assoc_zval(return_value, "operands", operands);
}
/* }}} */
static void bytekit_get_next_oplines(zend_op_array *ops, zend_op *op, int i, int len, int *next_t, int *next_f, int *exception TSRMLS_DC)
{
int tc;
char *opcodes_base = BYTEKIT_G(opcodes_base) ? BYTEKIT_G(opcodes_base) : ops->opcodes;
/* first assume we end */
*next_t = -1;
*next_f = -1;
*exception = -1;
/* next line(s) depend(s) on opcodes */
switch (op->opcode) {
default:
*next_t = i + len;
break;
case ZEND_GOTO:
case ZEND_JMP:
*next_t = (op->op1.u.jmp_addr - (zend_op *)opcodes_base);
break;
/* case ZEND_FE_RESET: <- handling FE_RESET correctly makes CFG unnecessary complex */
case ZEND_FE_FETCH:
*next_t = i + len;
*next_f = op->op2.u.opline_num;
break;
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
*next_f = i + len;
*next_t = (op->op2.u.jmp_addr - (zend_op *)opcodes_base);
break;
case ZEND_JMPZNZ:
*next_t = op->extended_value;
*next_f = op->op2.u.opline_num;
break;
case ZEND_BRK:
case ZEND_CONT:
{
zend_brk_cont_element *jmp_to;
int array_offset = op->op1.u.opline_num;
int nest_level = 1;
do {
if (array_offset < 0 || array_offset >= ops->last_brk_cont) {
break;
}
jmp_to = &ops->brk_cont_array[array_offset];
array_offset = jmp_to->parent;
} while (--nest_level > 0);
if (nest_level == 1) {
break;
}
if (op->opcode == ZEND_BRK) {
*next_t = jmp_to->brk;
} else {
*next_t = jmp_to->cont;
}
}
break;
case ZEND_EXIT:
case ZEND_RETURN:
break;
case ZEND_HANDLE_EXCEPTION:
break;
case ZEND_THROW:
/* search the try/catch array for the correct catch block */
for (tc=0; tc<ops->last_try_catch; tc++) {
if (ops->try_catch_array[tc].try_op > i) {
/* ignore all further blocks */
break;
}
if (i >= ops->try_catch_array[tc].try_op && i < ops->try_catch_array[tc].catch_op) {
*exception = ops->try_catch_array[tc].catch_op;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "bytekit_get_next_oplines: found throw outside of try/catch");
}
}
break;
}
/* special handling of last opcode before catch branch */
for (tc=0; tc<ops->last_try_catch; tc++) {
if (ops->try_catch_array[tc].try_op > i) {
/* ignore all further blocks */
break;
}
if (ops->try_catch_array[tc].catch_op == i+1) {
*exception = ops->try_catch_array[tc].catch_op;
}
}
}
/* {{{ bytekit_build_basic_blocks */
static void bytekit_build_basic_blocks(zval *return_value, zend_op_array *ops, long flags TSRMLS_DC)
{
zend_op *op;
int i = 0;
int *entries, *exits, *bb, *next_f, *next_t, *exception;
zval *a_entries, *a_exits, *a_bb, *a_next_f, *a_next_t, *a_exception, *a_cfg, **a_cfg_elements;
unsigned int numopcodes;
int oplines, bbid;
/* Allocate slots for all opcodes */
numopcodes = ops->last;
entries = (int *) ecalloc(numopcodes, sizeof(int));
exits = (int *) ecalloc(numopcodes, sizeof(int));
bb = (int *) ecalloc(numopcodes, sizeof(int));
next_t = (int *) ecalloc(numopcodes, sizeof(int));
next_f = (int *) ecalloc(numopcodes, sizeof(int));
exception = (int *) ecalloc(numopcodes, sizeof(int));
/* determine the number of entries and exits */
/* opcodes with zero or more than 1 entries */
/* start a new basic block, opcodes with zero */
/* or more than 1 exits end a basic block */
for (op = ops->opcodes, i = 0; op && i < ops->last; op++, i++) {
oplines = bytekit_get_opcode_oplines(ops, op TSRMLS_CC);
bytekit_get_next_oplines(ops, op, i, oplines, &next_t[i], &next_f[i], &exception[i] TSRMLS_CC);
if (next_t[i] != -1) {
exits[i]++;
entries[next_t[i]]++;
}
if (next_f[i] != -1) {
exits[i]++;
entries[next_f[i]]++;
}
if (exception[i] != -1) {
exits[i]++;
entries[exception[i]]++;
}
while (oplines > 1) {
i++;
entries[i] = -1;
exits[i] = -1;
op++;
oplines--;
}