-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbcompiler_zend.c
2100 lines (1883 loc) · 63.7 KB
/
bcompiler_zend.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
/*
+----------------------------------------------------------------------+
| PHP bcompiler |
+----------------------------------------------------------------------+
| Copyright (c) 2000-2001 Community Connect, Inc. (apc_* functions) |
| Copyright (c) 1997-2010 The PHP Group (remaining code) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Alan Knowles <[email protected]> |
| val khokhlov <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: bcompiler_zend.c 311909 2011-06-08 01:33:35Z alan_k $ */
#include "php_bcompiler.h"
void apc_serialize_zval(zval* zv, znode *zn TSRMLS_DC);
void apc_serialize_zval_ptr(zval** zv TSRMLS_DC);
void apc_create_zval(zval** zv TSRMLS_DC);
/* --- zend_llist ---------------------------------------------------------- */
static void store_zend_llist_element(void* arg, void* data TSRMLS_DC) {
int size = *((int*)arg);
STORE_BYTES((char*)data, size);
}
void apc_serialize_zend_llist(zend_llist* list TSRMLS_DC) {
char exists;
exists = (list != NULL) ? 1 : 0;
SERIALIZE_SCALAR(exists, char); /* write twice to remove need for peeking. */
if (!exists) {
return;
}
SERIALIZE_SCALAR(exists, char);
SERIALIZE_SCALAR(list->size, size_t);
/** old code: SERIALIZE_SCALAR(list->dtor, void*); */
if (BCOMPILERG(current_write) < 0x0005) SERIALIZE_SCALAR(0, ulong); /* dummy 4 bytes */
/** old code: SERIALIZE_SCALAR(list->persistent, unsigned char); */
SERIALIZE_SCALAR(list->persistent, zend_uchar);
SERIALIZE_SCALAR(zend_llist_count(list), int);
zend_llist_apply_with_argument(list, store_zend_llist_element, &list->size TSRMLS_CC);
}
void apc_deserialize_zend_llist(zend_llist* list TSRMLS_DC) {
char exists;
size_t size;
void (*dtor)(void*);
unsigned char persistent;
int count, i;
char* data;
DESERIALIZE_SCALAR(&exists, char);
assert(exists != 0);
/* read the list parameters */
DESERIALIZE_SCALAR(&size, size_t);
/** old code: DESERIALIZE_SCALAR(&dtor, void*); */
if (BCOMPILERG(current_version) < 0x0005) DESERIALIZE_VOID(ulong); /* dummy 4 bytes */
dtor = NULL;
/** old code: DESERIALIZE_SCALAR(&persistent, unsigned char); */
DESERIALIZE_SCALAR(&persistent, zend_uchar);
/* initialize the list */
zend_llist_init(list, size, dtor, persistent);
/* insert the list elements */
DESERIALIZE_SCALAR(&count, int);
data = (char*) emalloc(list->size);
for (i = 0; i < count; i++) {
LOAD_BYTES(data, list->size);
zend_llist_add_element(list, data);
}
efree(data);
}
void apc_create_zend_llist(zend_llist** list TSRMLS_DC)
{
char exists;
DESERIALIZE_SCALAR(&exists, char);
if (exists) {
*list = (zend_llist*) ecalloc(1, sizeof(zend_llist));
apc_deserialize_zend_llist(*list TSRMLS_CC);
}
else {
*list = 0;
}
}
/* --- HashTable ----------------------------------------------------------- */
static ulong pDestructor_idx(dtor_func_t p TSRMLS_DC) {
if (BCOMPILERG(current_write) >= 0x0005) {
if (p == ZVAL_PTR_DTOR) return 1;
if (p == ZEND_FUNCTION_DTOR) return 2;
if (p == ZEND_CLASS_DTOR) return 3;
#ifdef COMPILE_DL_BCOMPILER /* val: these may be missing in shared lib */
if (p == module_registry.pDestructor) return 4;
if (p == EG(zend_constants)->pDestructor) return 5;
#else
if (p == ZEND_MODULE_DTOR) return 4;
if (p == ZEND_CONSTANT_DTOR) return 5;
#endif
if (p == (dtor_func_t)free_estring) return 6;
#ifdef COMPILE_DL_BCOMPILER /* val: these may be missing in shared lib */
if (p == EG(regular_list).pDestructor) return 7;
if (p == EG(persistent_list).pDestructor) return 8;
#else
if (p == list_entry_destructor) return 7;
if (p == plist_entry_destructor) return 8;
#endif
}
return 0;
}
#ifdef ZEND_ENGINE_2
/* since zend_function is copied in deserialize_hashtable, we need to update
special class function pointers */
static void adjust_class_handler(zend_class_entry *zc, zend_function *old, zend_function *act) {
if (zc) {
if (zc->constructor == old) zc->constructor = act;
if (zc->destructor == old) zc->destructor = act;
if (zc->clone == old) zc->clone = act;
if (zc->__get == old) zc->__get = act;
if (zc->__set == old) zc->__set = act;
if (zc->__call == old) zc->__call = act;
#ifdef ZEND_ENGINE_2_1
if (zc->__unset == old) zc->__unset = act;
if (zc->__isset == old) zc->__isset = act;
if (zc->serialize_func == old) zc->serialize_func = act;
if (zc->unserialize_func == old) zc->unserialize_func = act;
#endif
#ifdef ZEND_ENGINE_2_2
if (zc->__tostring == old) zc->__tostring = act;
#endif
#ifdef ZEND_ENGINE_2_3
if (zc->__callstatic == old) zc->__callstatic = act;
#endif
}
}
#endif
void apc_serialize_hashtable(HashTable* ht, void* funcptr TSRMLS_DC)
{
char exists; /* for identifying null lists */
Bucket* p;
void (*serialize_bucket)(void* TSRMLS_DC);
serialize_bucket = (void(*)(void* TSRMLS_DC)) funcptr;
exists = (ht != NULL) ? 1 : 0;
SERIALIZE_SCALAR(exists, char);
if (!exists) {
return;
}
/* Serialize the hash meta-data. */
SERIALIZE_SCALAR(ht->nTableSize, uint);
/** old code: SERIALIZE_SCALAR(ht->pDestructor, void*); */
SERIALIZE_SCALAR(pDestructor_idx(ht->pDestructor TSRMLS_CC), ulong);
SERIALIZE_SCALAR(ht->nNumOfElements, uint);
SERIALIZE_SCALAR(ht->persistent, int);
#if PHP_MAJOR_VERSION >= 6
SERIALIZE_SCALAR(ht->unicode, zend_bool);
#endif
/* Iterate through the buckets of the hash, serializing as we go. */
p = ht->pListHead;
while(p != NULL) {
SERIALIZE_SCALAR(p->h, ulong);
SERIALIZE_SCALAR(p->nKeyLength,uint);
#if PHP_MAJOR_VERSION >= 6
SERIALIZE_SCALAR(p->key.type,zend_uchar);
apc_serialize_zstring(p->key.arKey.s, USTR_BYTES(p->key.type, p->nKeyLength) TSRMLS_CC);
#else
apc_serialize_zstring(p->arKey, p->nKeyLength TSRMLS_CC);
#endif
serialize_bucket(p->pData TSRMLS_CC );
p = p->pListNext;
}
}
void apc_deserialize_hashtable(HashTable* ht, void* funcptr, void* dptr, int datasize, char exists TSRMLS_DC)
{
uint nSize;
dtor_func_t pDestructor;
uint nNumOfElements;
int persistent;
int j;
ulong h;
uint nKeyLength;
char* arKey;
#if PHP_MAJOR_VERSION >= 6
zend_uchar keyType;
#endif
void* pData;
int status;
void (*deserialize_bucket)(void* TSRMLS_DC);
void (*free_bucket)(void*);
BCOMPILER_DEBUG(("-----------------------------\nHASH TABLE:\n"));
deserialize_bucket = (void(*)(void* TSRMLS_DC)) funcptr;
free_bucket = (void(*)(void*)) dptr;
assert(exists != 0);
DESERIALIZE_SCALAR(&nSize, uint);
/** old code: DESERIALIZE_SCALAR(&pDestructor, void*); */
DESERIALIZE_SCALAR(&h, ulong);
pDestructor = NULL;
if (BCOMPILERG(current_version) >= 0x0005) switch (h) {
case 1: pDestructor = ZVAL_PTR_DTOR; break;
case 2: pDestructor = ZEND_FUNCTION_DTOR; break;
case 3: pDestructor = ZEND_CLASS_DTOR; break;
#ifdef COMPILE_DL_BCOMPILER /* val: these may be missing in shared lib */
case 4: pDestructor = module_registry.pDestructor; break;
case 5: pDestructor = EG(zend_constants)->pDestructor; break;
#else
case 4: pDestructor = ZEND_MODULE_DTOR; break;
case 5: pDestructor = ZEND_CONSTANT_DTOR; break;
#endif
case 6: pDestructor = (dtor_func_t)free_estring; break;
#ifdef COMPILE_DL_BCOMPILER /* val: these may be missing in shared lib */
case 7: pDestructor = EG(regular_list).pDestructor; break;
case 8: pDestructor = EG(persistent_list).pDestructor; break;
#else
case 7: pDestructor = list_entry_destructor; break;
case 8: pDestructor = plist_entry_destructor; break;
#endif
}
DESERIALIZE_SCALAR(&nNumOfElements,uint);
DESERIALIZE_SCALAR(&persistent, int);
BCOMPILER_DEBUG(("-----------------------------\nBEGIN TABLE:\n"));
/* Although the hash is already allocated (we're a deserialize, not a
* create), we still need to initialize it. If this fails, something
* very very bad happened. */
status = zend_hash_init(ht, nSize, NULL, pDestructor, persistent);
assert(status != FAILURE);
#if PHP_MAJOR_VERSION >= 6
DESERIALIZE_SCALAR(&ht->unicode, zend_bool);
#endif
/* Luckily, the number of elements in a hash is part of its struct, so
* we can just deserialize that many hashtable elements. */
for (j = 0; j < (int) nNumOfElements; j++) {
DESERIALIZE_SCALAR(&h, ulong);
DESERIALIZE_SCALAR(&nKeyLength, uint);
#if PHP_MAJOR_VERSION >= 6
DESERIALIZE_SCALAR(&keyType, zend_uchar);
apc_create_string_u(&arKey, -1 TSRMLS_CC);
#else
apc_create_string(&arKey TSRMLS_CC);
#endif
deserialize_bucket(&pData TSRMLS_CC);
/* val: return pData = NULL to skip element */
if (!pData && arKey) { efree(arKey); continue; }
/* If nKeyLength is non-zero, this element is a hashed key/value
* pair. Otherwise, it is an array element with a numeric index */
BCOMPILER_DEBUG(("-----------------------------\nFUNC %s\n",arKey));
if (nKeyLength != 0) {
if(datasize == sizeof(void*)) {
#if PHP_MAJOR_VERSION >= 6
BCOMPILER_DEBUGFULL(("adding element [ptr] type=%d len=%d ptr=%p\n", keyType, nKeyLength, &pData));
_zend_u_hash_add_or_update(ht, keyType, ZSTR(arKey), nKeyLength, &pData,
datasize, NULL, HASH_ADD ZEND_FILE_LINE_CC);
#elif defined(ZEND_ENGINE_2)
BCOMPILER_DEBUGFULL(("adding element [ptr] %s = %p\n", arKey, &pData));
_zend_hash_add_or_update(ht, arKey, nKeyLength, &pData,
datasize, NULL, HASH_ADD ZEND_FILE_LINE_CC);
#else
zend_hash_add_or_update(ht, arKey, nKeyLength, &pData,
datasize, NULL, HASH_ADD);
#endif
} else {
#ifdef ZEND_ENGINE_2
void *pDest;
#endif
BCOMPILER_DEBUGFULL(("adding element [cpy] %s = %p\n", arKey, pData));
#ifdef ZEND_ENGINE_2
# if PHP_MAJOR_VERSION >= 6
_zend_u_hash_add_or_update(ht, keyType, ZSTR(arKey), nKeyLength, pData,
datasize, &pDest, HASH_ADD ZEND_FILE_LINE_CC);
# else
_zend_hash_add_or_update(ht, arKey, nKeyLength, pData,
datasize, &pDest, HASH_ADD ZEND_FILE_LINE_CC);
# endif
BCOMPILER_DEBUGFULL(("actual added element ptr=%p\n", pDest));
adjust_class_handler(BCOMPILERG(cur_zc), pData, pDest);
#else
zend_hash_add_or_update(ht, arKey, nKeyLength, pData,
datasize, NULL, HASH_ADD);
#endif
}
} else { /* numeric index, not key string */
if(datasize == sizeof(void*)) {
zend_hash_index_update(ht, h, &pData, datasize, NULL);
} else {
zend_hash_index_update(ht, h, pData, datasize, NULL);
}
}
if (dptr != NULL) {
free_bucket(&pData);
}
/* else {
BCOMPILER_DEBUG((stderr, "Do not free: %s\n", arKey));
} */
if (arKey) efree(arKey); /* ADDED BY MPB */
}
}
void apc_create_hashtable(HashTable** ht, void* funcptr, void* dptr, int datasize TSRMLS_DC)
{
char exists; /* for identifying null hashtables */
/*PEEK_SCALAR(&exists, char); */
DESERIALIZE_SCALAR(&exists, char);
if (exists==1) {
*ht = (HashTable*) ecalloc(1, sizeof(HashTable));
apc_deserialize_hashtable(*ht, funcptr, dptr, datasize,exists TSRMLS_CC);
} else {
/* DESERIALIZE_SCALAR(&exists, char); */
*ht = NULL;
}
}
/* --- zvalue_value -------------------------------------------------------- */
#define IS_MANGLED_FILE 0x81
#define IS_MANGLED_DIR 0x82
/* serialize a suspected __FILE__ or __DIR__ entry */
static int bc_serialize_filedir(zvalue_value* zv, zend_uchar type TSRMLS_DC) {
int done = 0;
/* quick check for the string type and bytecode version */
if (!BCOMPILERG(detect_filedir) || type != IS_STRING) return 0;
if (BCOMPILERG(current_write) < 25) return 0;
/* proceed if the current filename is known */
if (BCOMPILERG(zoa) && BCOMPILERG(zoa)->filename) {
#ifdef ZEND_ENGINE_2_3
char *dir = estrdup(BCOMPILERG(zoa)->filename);
size_t n = zend_dirname(dir, strlen(dir));
#endif
if (!strcmp(zv->str.val, BCOMPILERG(zoa)->filename)) {
BCOMPILER_DEBUG(("Mangling possible __FILE__ entry: %s\n", zv->str.val));
SERIALIZE_SCALAR(IS_MANGLED_FILE, zend_uchar);
done = 1;
}
#ifdef ZEND_ENGINE_2_3
/* to prevent false dir detections, proceed with lengths >= 8 */
else if (n >= 8 && !strcmp(zv->str.val, dir)) {
BCOMPILER_DEBUG(("Mangling possible __DIR__ entry: %s\n", dir));
SERIALIZE_SCALAR(IS_MANGLED_DIR, zend_uchar);
done = 1;
}
efree(dir);
#endif
}
return done;
}
/* deserialize a stored __FILE__ or __DIR__ entry */
static int bc_deserialize_filedir(zvalue_value* zv, zend_uchar *type TSRMLS_DC) {
/* quick check it's not we're looking for */
if (BCOMPILERG(current_version) < 25) return 0;
if (*type != IS_MANGLED_FILE && *type != IS_MANGLED_DIR) return 0;
/* create a string constant */
if (zv->str.val) efree(zv->str.val);
if (*type == IS_MANGLED_FILE) {
zv->str.val = estrdup( BCOMPILERG(current_filename) );
BCOMPILER_DEBUG(("Demangling possible __FILE__ entry: %s\n", zv->str.val));
}
else if (*type == IS_MANGLED_DIR) {
#ifdef ZEND_ENGINE_2_3
char *dir = estrdup( BCOMPILERG(current_filename) );
zend_dirname(dir, strlen(dir));
zv->str.val = dir;
BCOMPILER_DEBUG(("Demangling possible __DIR__ entry: %s\n", zv->str.val));
#else
zv->str.val = estrdup("__DIR__");
BCOMPILER_DEBUG(("Error: __DIR__ is unavailable for the current Zend version\n"));
#endif
}
zv->str.len = strlen(zv->str.val);
*type = IS_STRING;
return 1;
}
void apc_serialize_zvalue_value(zvalue_value* zv, zend_uchar type, znode *zn TSRMLS_DC)
{
/* A zvalue_value is a union, and as such we first need to
* determine exactly what it's type is, then serialize the
* appropriate structure. */
#if defined(IS_CONSTANT_TYPE_MASK)
type &= IS_CONSTANT_TYPE_MASK;
#elif defined(IS_CONSTANT_INDEX)
type &= ~IS_CONSTANT_INDEX;
#endif
/* temp hack - PHP6 seems to mix unicode and non-unicode IS_CONSTANT */
#if PHP_MAJOR_VERSION >= 6
if (type == IS_CONSTANT) type = UG(unicode) ? IS_UNICODE : IS_STRING;
#endif
switch (type)
{
case IS_RESOURCE:
case IS_BOOL:
case IS_LONG:
SERIALIZE_SCALAR(zv->lval, long);
break;
case IS_DOUBLE:
SERIALIZE_SCALAR(zv->dval, double);
break;
case IS_NULL:
/* null value - it's used for temp_vars */
#if defined(ZEND_ENGINE_2) || defined(ZEND_ENGINE_2_1)
if (zn && BCOMPILERG(current_write) >= 0x0008) {
# ifdef ZEND_ENGINE_2_4
SERIALIZE_SCALAR(zn->EA, zend_uint);
# else
SERIALIZE_SCALAR(zn->u.EA.var, zend_uint);
SERIALIZE_SCALAR(zn->u.EA.type, zend_uint);
# endif
}
#endif
if (BCOMPILERG(current_write) >= 0x0005) SERIALIZE_SCALAR(zv->lval, long);
break;
case IS_CONSTANT:
case IS_STRING:
#ifndef ZEND_ENGINE_2
case FLAG_IS_BC:
#endif
apc_serialize_zstring(zv->str.val, zv->str.len TSRMLS_CC);
SERIALIZE_SCALAR(zv->str.len, int);
break;
#if PHP_MAJOR_VERSION >= 6
case IS_UNICODE:
apc_serialize_zstring((char*)zv->ustr.val, UBYTES(zv->ustr.len) TSRMLS_CC);
SERIALIZE_SCALAR(zv->ustr.len, int);
break;
#endif
case IS_ARRAY:
apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC);
break;
case IS_CONSTANT_ARRAY:
apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC);
break;
case IS_OBJECT:
#ifdef ZEND_ENGINE_2
/* not yet!
apc_serialize_zend_class_entry(zv->obj.handlers, NULL, 0, NULL, 0 TSRMLS_CC);
apc_serialize_hashtable(zv->obj.properties, apc_serialize_zval_ptr TSRMLS_CC);
*/
#else
apc_serialize_zend_class_entry(zv->obj.ce, NULL, 0, NULL, 0 TSRMLS_CC);
#endif
break;
#ifdef ZEND_ENGINE_2_4
/*case IS_CLASS:
case IS_SCALAR:
case IS_NUMERIC:
break;*/
#endif
default:
/* The above list enumerates all types. If we get here,
* something very very bad has happened. */
assert(0);
}
}
void apc_deserialize_zvalue_value(zvalue_value* zv, zend_uchar type, znode *zn TSRMLS_DC)
{
/* We peeked ahead in the calling routine to deserialize the
* type. Now we just deserialize. */
#if defined(IS_CONSTANT_TYPE_MASK)
type &= IS_CONSTANT_TYPE_MASK;
#elif defined(IS_CONSTANT_INDEX)
type &= ~IS_CONSTANT_INDEX;
#endif
/* temp hack - PHP6 seems to mix unicode and non-unicode IS_CONSTANT */
#if PHP_MAJOR_VERSION >= 6
if (type == IS_CONSTANT) type = BCOMPILERG(is_unicode) ? IS_UNICODE : IS_STRING;
#endif
switch(type)
{
case IS_RESOURCE:
case IS_BOOL:
case IS_LONG:
DESERIALIZE_SCALAR(&zv->lval, long);
break;
case IS_NULL:
/* null value - it's used for temp_vars */
#if defined(ZEND_ENGINE_2) || defined(ZEND_ENGINE_2_1)
if (zn && BCOMPILERG(current_version) >= 0x0008) {
# ifdef ZEND_ENGINE_2_4
DESERIALIZE_SCALAR(&zn->EA, zend_uint);
# else
DESERIALIZE_SCALAR(&zn->u.EA.var, zend_uint);
DESERIALIZE_SCALAR(&zn->u.EA.type, zend_uint);
# endif
}
#endif
if (BCOMPILERG(current_version) >= 0x0005) DESERIALIZE_SCALAR(&zv->lval, long);
break;
case IS_DOUBLE:
DESERIALIZE_SCALAR(&zv->dval, double);
break;
case IS_CONSTANT:
case IS_STRING:
#ifndef ZEND_ENGINE_2
case FLAG_IS_BC:
#endif
#if PHP_MAJOR_VERSION >= 6
apc_create_string_u(&zv->str.val, 0 TSRMLS_CC);
#else
apc_create_string(&zv->str.val TSRMLS_CC);
#endif
DESERIALIZE_SCALAR(&zv->str.len, int);
break;
#if PHP_MAJOR_VERSION >= 6
case IS_UNICODE:
apc_create_string_u((char**)&zv->ustr.val, 1 TSRMLS_CC);
DESERIALIZE_SCALAR(&zv->ustr.len, int);
break;
#endif
case IS_ARRAY:
apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC);
break;
case IS_CONSTANT_ARRAY:
apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC);
break;
case IS_OBJECT:
#ifndef ZEND_ENGINE_2
apc_create_zend_class_entry(&zv->obj.ce, NULL, NULL TSRMLS_CC);
apc_create_hashtable(&zv->obj.properties, apc_create_zval, NULL, sizeof(zval *) TSRMLS_CC);
#endif
break;
#ifdef ZEND_ENGINE_2_4
/*case IS_CLASS:
case IS_SCALAR:
case IS_NUMERIC:
break;*/
#endif
default:
/* The above list enumerates all types. If we get here,
* something very very bad has happened. */
assert(0);
}
}
/* --- zval ---------------------------------------------------------------- */
void apc_serialize_zval_ptr(zval** zv TSRMLS_DC)
{
apc_serialize_zval(*zv, NULL TSRMLS_CC);
}
void apc_serialize_zval(zval* zv, znode *zn TSRMLS_DC)
{
/* type is the switch for serializing zvalue_value */
if (!bc_serialize_filedir(&zv->value, zv->type TSRMLS_CC)) {
SERIALIZE_SCALAR(zv->type, zend_uchar);
apc_serialize_zvalue_value(&zv->value, zv->type, zn TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2_3
SERIALIZE_SCALAR(zv->is_ref__gc, zend_uchar);
SERIALIZE_SCALAR(zv->refcount__gc, zend_ushort);
#else
SERIALIZE_SCALAR(zv->is_ref, zend_uchar);
SERIALIZE_SCALAR(zv->refcount, zend_ushort);
#endif
}
void apc_deserialize_zval(zval* zv, znode *zn TSRMLS_DC)
{
/* type is the switch for deserializing zvalue_value */
DESERIALIZE_SCALAR(&zv->type, zend_uchar);
if (!bc_deserialize_filedir(&zv->value, &zv->type TSRMLS_CC)) {
apc_deserialize_zvalue_value(&zv->value, zv->type, zn TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2_3
DESERIALIZE_SCALAR(&zv->is_ref__gc, zend_uchar);
DESERIALIZE_SCALAR(&zv->refcount__gc, zend_ushort);
#else
DESERIALIZE_SCALAR(&zv->is_ref, zend_uchar);
DESERIALIZE_SCALAR(&zv->refcount, zend_ushort);
#endif
}
void apc_create_zval(zval** zv TSRMLS_DC)
{
#ifdef ZEND_ENGINE_2_2
ALLOC_ZVAL(*zv);
#else
*zv = (zval*) emalloc(sizeof(zval));
#endif
memset(*zv, 0, sizeof(zval));
apc_deserialize_zval(*zv, NULL TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2
/* --- arg_info ------------------------------------------------------------ */
void apc_serialize_arg_info(zend_arg_info* arg_info TSRMLS_DC)
{
if (arg_info == NULL) {
SERIALIZE_SCALAR(0, char);
return; /* arg_types is null */
}
SERIALIZE_SCALAR(1, char);
SERIALIZE_SCALAR(arg_info->name_len , zend_uint);
/* apc_serialize_string(arg_info->name TSRMLS_CC);*/
apc_serialize_zstring(ZS2S(arg_info->name), ZLEN(arg_info->name_len) TSRMLS_CC);
SERIALIZE_SCALAR(arg_info->class_name_len , zend_uint);
if (arg_info->class_name_len) {
/* apc_serialize_string(arg_info->class_name TSRMLS_CC);*/
apc_serialize_zstring(ZS2S(arg_info->class_name), ZLEN(arg_info->class_name_len) TSRMLS_CC);
}
SERIALIZE_SCALAR(arg_info->allow_null , zend_bool);
SERIALIZE_SCALAR(arg_info->pass_by_reference, zend_bool);
//SERIALIZE_SCALAR(arg_info->return_reference , zend_bool);
//SERIALIZE_SCALAR(arg_info->required_num_args, int);
#ifdef ZEND_ENGINE_2_1
if (BCOMPILERG(current_write) > 20) {
# ifdef ZEND_ENGINE_2_4
SERIALIZE_SCALAR(arg_info->type_hint, zend_uint);
# else
SERIALIZE_SCALAR(arg_info->array_type_hint, zend_bool);
# endif
}
#endif
}
void apc_create_arg_info(zend_arg_info* arg_info TSRMLS_DC)
{
char exists;
DESERIALIZE_SCALAR(&exists, char);
if (!exists) {
arg_info = NULL;
return; /* arg_types is null */
}
DESERIALIZE_SCALAR(&arg_info->name_len, zend_uint);
apc_create_string_u(ZS2SP(arg_info->name), -1 TSRMLS_CC);
DESERIALIZE_SCALAR(&arg_info->class_name_len, zend_uint);
if (arg_info->class_name_len) {
apc_create_string_u(ZS2SP(arg_info->class_name), -1 TSRMLS_CC);
}
else ZS2SR(arg_info->class_name) = NULL;
DESERIALIZE_SCALAR(&arg_info->allow_null, zend_bool);
DESERIALIZE_SCALAR(&arg_info->pass_by_reference, zend_bool);
//DESERIALIZE_SCALAR(&arg_info->return_reference, zend_bool);
//DESERIALIZE_SCALAR(&arg_info->required_num_args, int);
#ifdef ZEND_ENGINE_2_1
if (BCOMPILERG(current_version) > 20) {
# ifdef ZEND_ENGINE_2_4
DESERIALIZE_SCALAR(&arg_info->type_hint, zend_uint);
# else
DESERIALIZE_SCALAR(&arg_info->array_type_hint, zend_bool);
# endif
}
#endif
}
#else /* arg_info */
/* --- arg_types ----------------------------------------------------------- */
void apc_serialize_arg_types(zend_uchar* arg_types TSRMLS_DC)
{
if (arg_types == NULL) {
SERIALIZE_SCALAR(0, char);
return; /* arg_types is null */
}
SERIALIZE_SCALAR(1, char);
SERIALIZE_SCALAR(arg_types[0], zend_uchar);
STORE_BYTES(arg_types + 1, arg_types[0]*sizeof(zend_uchar));
}
void apc_create_arg_types(zend_uchar** arg_types TSRMLS_DC)
{
char exists;
zend_uchar count;
DESERIALIZE_SCALAR(&exists, char);
if (!exists) {
*arg_types = NULL;
return; /* arg_types is null */
}
DESERIALIZE_SCALAR(&count, zend_uchar);
*arg_types = emalloc(count + 1);
(*arg_types)[0] = count;
LOAD_BYTES((*arg_types) + 1, count*sizeof(zend_uchar));
}
#endif /* arg_types */
/* --- zend_function_entry ------------------------------------------------- */
void apc_serialize_zend_function_entry(zend_function_entry* zfe TSRMLS_DC)
{
#ifdef ZEND_ENGINE_2
int i;
#endif
#if PHP_MAJOR_VERSION >= 6
int len;
if (zfe->fname == NULL) len = -1;
else if (UG(unicode)) len = u_strlen((UChar*)zfe->fname) * sizeof(UChar);
else len = strlen(zfe->fname);
apc_serialize_zstring((char*)zfe->fname, len TSRMLS_CC);
#else
apc_serialize_string((char*)zfe->fname TSRMLS_CC);
#endif
BCOMPILER_DEBUG(("serializing function %s\n",zfe->fname ));
/** old code: SERIALIZE_SCALAR(zfe->handler, void*); */
if (BCOMPILERG(current_write) < 0x0005) SERIALIZE_SCALAR(0, ulong); /* dummy 4 bytes */
#ifdef ZEND_ENGINE_2
SERIALIZE_SCALAR(zfe->num_args, int);
for (i=0; i< zfe->num_args; i++) {
apc_serialize_arg_info((zend_arg_info *)&zfe->arg_info[i] TSRMLS_CC);
}
#else
apc_serialize_arg_types(zfe->func_arg_types TSRMLS_CC);
#endif
}
void apc_deserialize_zend_function_entry(zend_function_entry* zfe TSRMLS_DC)
{
#ifdef ZEND_ENGINE_2
int i;
#endif
apc_create_string_u((char**)&zfe->fname, -1 TSRMLS_CC);
/** old code: DESERIALIZE_SCALAR(&zfe->handler, void*); */
if (BCOMPILERG(current_version) < 0x0005) DESERIALIZE_VOID(ulong); /* dummy 4 bytes */
zfe->handler = NULL;
BCOMPILER_DEBUG(("deserializing function %s\n",zfe->handler));
#ifdef ZEND_ENGINE_2
DESERIALIZE_SCALAR(&zfe->num_args, int);
zfe->arg_info = (zend_arg_info *) ecalloc(zfe->num_args, sizeof(zend_arg_info));
for (i=0; i< zfe->num_args; i++) {
apc_create_arg_info((zend_arg_info*)&zfe->arg_info[i] TSRMLS_CC);
}
#else
apc_create_arg_types(&zfe->func_arg_types TSRMLS_CC);
#endif
}
#ifdef ZEND_ENGINE_2
/* --- zend_property_info -------------------------------------------------- */
void apc_serialize_zend_property_info(zend_property_info* zpr TSRMLS_DC)
{
SERIALIZE_SCALAR(zpr->flags, zend_uint);
#if PHP_MAJOR_VERSION >= 6
apc_serialize_zstring(ZS2S(zpr->name), ZLEN(zpr->name_length) TSRMLS_CC);
#else
apc_serialize_zstring(zpr->name, zpr->name_length TSRMLS_CC);
#endif
SERIALIZE_SCALAR(zpr->name_length, uint);
SERIALIZE_SCALAR(zpr->h, ulong);
}
void apc_deserialize_zend_property_info(zend_property_info* zpr TSRMLS_DC)
{
DESERIALIZE_SCALAR(&zpr->flags, zend_uint);
apc_create_string_u(ZS2SP(zpr->name), -1 TSRMLS_CC);
DESERIALIZE_SCALAR(&zpr->name_length, uint);
DESERIALIZE_SCALAR(&zpr->h, ulong);
#ifdef ZEND_ENGINE_2_2
zpr->ce = BCOMPILERG(cur_zc);
#endif
}
void apc_create_zend_property_info(zend_property_info** zf TSRMLS_DC)
{
*zf = (zend_property_info*) ecalloc(1, sizeof(zend_property_info));
apc_deserialize_zend_property_info(*zf TSRMLS_CC);
}
void apc_free_zend_property_info(zend_property_info** zf TSRMLS_DC)
{
if (*zf != NULL) {
efree(*zf);
}
*zf = NULL;
}
#else /* zend_property_info */
/* --- zend_property_reference --------------------------------------------- */
void apc_serialize_zend_property_reference(zend_property_reference* zpr TSRMLS_DC)
{
SERIALIZE_SCALAR(zpr->type, int);
apc_serialize_zval(zpr->object, NULL TSRMLS_CC);
apc_serialize_zend_llist(zpr->elements_list TSRMLS_CC);
}
void apc_deserialize_zend_property_reference(zend_property_reference* zpr TSRMLS_DC)
{
DESERIALIZE_SCALAR(&zpr->type, int);
apc_deserialize_zval(zpr->object, NULL TSRMLS_CC);
apc_create_zend_llist(&zpr->elements_list TSRMLS_CC);
}
#endif /* zend_property_reference */
#ifndef ZEND_ENGINE_2
/* --- zend_overloaded_element --------------------------------------------- */
void apc_serialize_zend_overloaded_element(zend_overloaded_element* zoe TSRMLS_DC)
{
SERIALIZE_SCALAR(zoe->type, zend_uchar);
apc_serialize_zval(&zoe->element, NULL TSRMLS_CC);
}
void apc_deserialize_zend_overloaded_element(zend_overloaded_element* zoe TSRMLS_DC)
{
DESERIALIZE_SCALAR(&zoe->type, zend_uchar);
apc_deserialize_zval(&zoe->element, NULL TSRMLS_CC);
}
#endif
/* --- zend_class_entry ---------------------------------------------------- */
void apc_serialize_zend_class_entry(zend_class_entry* zce , char* force_parent_name, int force_parent_len, char* force_key, int force_key_len TSRMLS_DC)
{
zend_function_entry* zfe;
int count, i;
BCOMPILER_DEBUG(("adding type : %i\n",zce->type ));
SERIALIZE_SCALAR(zce->type, char);
#if PHP_MAJOR_VERSION >= 6
apc_serialize_zstring(ZS2S(zce->name), ZLEN(zce->name_length) TSRMLS_CC);
#else
apc_serialize_zstring(zce->name, zce->name_length TSRMLS_CC);
#endif
SERIALIZE_SCALAR(zce->name_length, uint);
/* Serialize the name of this class's parent class (if it has one)
* so that we can perform inheritance during deserialization (see
* apc_deserialize_zend_class_entry). */
if (zce->parent != NULL) {
/*Parent*/
SERIALIZE_SCALAR(1, char);
#if PHP_MAJOR_VERSION >= 6
apc_serialize_zstring(ZS2S(zce->parent->name), ZLEN(zce->parent->name_length) TSRMLS_CC);
#else
apc_serialize_zstring(zce->parent->name, zce->parent->name_length TSRMLS_CC);
#endif
} else if (force_parent_len > 0) {
SERIALIZE_SCALAR(1, char);
apc_serialize_zstring(force_parent_name, force_parent_len TSRMLS_CC);
} else {
SERIALIZE_SCALAR(0, char);
}
#ifdef ZEND_ENGINE_2
SERIALIZE_SCALAR(zce->refcount , int);
#else
SERIALIZE_SCALAR(*(zce->refcount) , int);
#endif
//SERIALIZE_SCALAR(zce->constants_updated, zend_bool);
#ifdef ZEND_ENGINE_2
SERIALIZE_SCALAR(zce->ce_flags, zend_uint);
#endif
BCOMPILER_DEBUG(("-----------------------------\nFUNC TABLE:\n"));
BCOMPILERG(cur_zc) = zce;
apc_serialize_hashtable(&zce->function_table, apc_serialize_zend_function TSRMLS_CC);
BCOMPILERG(cur_zc) = NULL;
#ifndef ZEND_ENGINE_2_4 /* todo */
BCOMPILER_DEBUG(("-----------------------------\nVARS:\n"));
apc_serialize_hashtable(&zce->default_properties, apc_serialize_zval_ptr TSRMLS_CC);
#endif
#ifdef ZEND_ENGINE_2
BCOMPILER_DEBUG(("-----------------------------\nPROP INFO:\n"));
apc_serialize_hashtable(&zce->properties_info, apc_serialize_zend_property_info TSRMLS_CC);
# ifndef ZEND_ENGINE_2_4 /* todo */
# ifdef ZEND_ENGINE_2_1
/* new for 0.12 */
if (BCOMPILERG(current_write) >= 0x000c) {
BCOMPILER_DEBUG(("-----------------------------\nDEFAULT STATIC MEMBERS:\n"));
apc_serialize_hashtable(&zce->default_static_members, apc_serialize_zval_ptr TSRMLS_CC);
}
# endif
# endif
# ifndef ZEND_ENGINE_2_4 /* todo */
/* not sure if statics should be dumped... (val: surely not for ZE v2.1) */
BCOMPILER_DEBUG(("-----------------------------\nSTATICS?:\n"));
# ifdef ZEND_ENGINE_2_1
apc_serialize_hashtable(NULL, apc_serialize_zval_ptr TSRMLS_CC);
# else
apc_serialize_hashtable(zce->static_members, apc_serialize_zval_ptr TSRMLS_CC);
# endif
# endif
BCOMPILER_DEBUG(("-----------------------------\nCONSTANTS?:\n"));
apc_serialize_hashtable(&zce->constants_table, apc_serialize_zval_ptr TSRMLS_CC);
#endif
/* zend_class_entry.builtin_functions: this array appears to be
* terminated by an element where zend_function_entry.fname is null
* First we count the number of elements, then we serialize that count
* and all the functions. */
BCOMPILER_DEBUG(("-----------------------------\nBUILTIN:\n"));
count = 0;
if (zce->type == ZEND_INTERNAL_CLASS && zce->info.internal.builtin_functions) {
for (zfe = (zend_function_entry*)zce->info.internal.builtin_functions; zfe->fname != NULL; zfe++) {
count++;
}
}
SERIALIZE_SCALAR(count, int);
for (i = 0; i < count; i++) {
apc_serialize_zend_function_entry((zend_function_entry *)&zce->info.internal.builtin_functions[i] TSRMLS_CC);
}
#ifndef ZEND_ENGINE_2
/** old code: SERIALIZE_SCALAR(zce->handle_function_call, void*); */
/** old code: SERIALIZE_SCALAR(zce->handle_property_get, void*); */
/** old code: SERIALIZE_SCALAR(zce->handle_property_set, void*); */
if (BCOMPILERG(current_write) < 0x0005) {
SERIALIZE_SCALAR(0, ulong); /* zce->handle_function_call */
SERIALIZE_SCALAR(0, ulong); /* zce->handle_property_get */
SERIALIZE_SCALAR(0, ulong); /* zce->handle_property_set */
}
#endif
/* serialize interfaces */
#ifdef ZEND_ENGINE_2
if (BCOMPILERG(current_write) >= 0x0005) {
SERIALIZE_SCALAR(zce->num_interfaces, zend_uint);
if (BCOMPILERG(current_write) >= 25) {
for (i = 0; i < zce->num_interfaces; i++) {
if (!zce->interfaces[i]) {
apc_serialize_string(NULL TSRMLS_CC);
continue;
}
#if PHP_MAJOR_VERSION >= 6
apc_serialize_zstring(ZS2S(zce->interfaces[i]->name), ZLEN(zce->interfaces[i]->name_length) TSRMLS_CC);
#else
apc_serialize_zstring(zce->interfaces[i]->name, zce->interfaces[i]->name_length TSRMLS_CC);
#endif
}
}
}
#endif
/* do reflection info (file/start/end) !! TODO: check for PHP6 !! */
#ifdef ZEND_ENGINE_2
if (BCOMPILERG(current_write) >= 25) {
char* fname = bcompiler_handle_filename(zce->info.user.filename TSRMLS_CC);
apc_serialize_string(fname TSRMLS_CC);
SERIALIZE_SCALAR(zce->info.user.line_start, int);
SERIALIZE_SCALAR(zce->info.user.line_end, int);
apc_serialize_zstring(zce->info.user.doc_comment, zce->info.user.doc_comment_len TSRMLS_CC);
SERIALIZE_SCALAR(zce->info.user.doc_comment_len, int);
}
#endif
/* val: new!! add hash key for the class if set, 0 otherwise */
if (BCOMPILERG(current_write) >= 0x0005) {
if (force_key && force_key_len) {
char tmp = force_key_len > 0x7f ? 1 : force_key_len;
#if PHP_MAJOR_VERSION >= 6
if (force_key_len < 0) {
if (force_key_len < -0x7f) tmp = -1;
SERIALIZE_SCALAR(-tmp, char);
apc_serialize_zstring(force_key, UBYTES(-force_key_len) TSRMLS_CC);
} else {
#endif
SERIALIZE_SCALAR(tmp, char);
apc_serialize_zstring(force_key, force_key_len TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 6
}