forked from eaccelerator/eaccelerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathea_store.c
1156 lines (1004 loc) · 39.3 KB
/
ea_store.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
/*
+----------------------------------------------------------------------+
| eAccelerator project |
+----------------------------------------------------------------------+
| Copyright (c) 2004 - 2012 eAccelerator |
| http://eaccelerator.net |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| MA 02111-1307, USA. |
| |
| A copy is availble at http://www.gnu.org/copyleft/gpl.txt |
+----------------------------------------------------------------------+
$Id: ea_store.c 418 2010-06-03 11:13:47Z hans $
*/
#include "eaccelerator.h"
#ifdef HAVE_EACCELERATOR
#include "ea_store.h"
#include "debug.h"
/******************************************************************************/
/* Functions to calculate the size of different structure that a compiled php */
/* script contains. */
/* Each function needs to return a size that is aligned to the machine word */
/* size. */
/******************************************************************************/
// add the given length to the size var and aling it
#define ADDSIZE(size, len) (size) += (len); \
EA_SIZE_ALIGN(size);
#ifndef DEBUG
inline
#endif
#ifdef ZEND_ENGINE_2_3
static size_t calc_string(const char *str, int len TSRMLS_DC)
#else
static size_t calc_string(char *str, int len TSRMLS_DC)
#endif
{
if (len > MAX_DUP_STR_LEN || zend_hash_add(&EAG(strings), str, len, (void *) &str, sizeof(char *), NULL) == SUCCESS) {
EA_SIZE_ALIGN(len);
return len;
}
return 0;
}
typedef size_t (*calc_bucket_t) (void * TSRMLS_DC);
#define calc_hash_ex(from, start, calc_bucket) \
calc_hash_int(from, start, calc_bucket TSRMLS_CC)
#define calc_hash(from, calc_bucket) \
calc_hash_ex(from, (from)->pListHead, calc_bucket)
#define calc_zval_hash(from) \
calc_hash(from, (calc_bucket_t)calc_zval_ptr)
#define calc_zval_hash_ex(from, start) \
calc_hash_ex(from, start, (calc_bucket_t)calc_zval_ptr)
static size_t calc_zval_ptr(zval ** from TSRMLS_DC)
{
size_t size = 0;
ADDSIZE(size, sizeof(zval));
size += calc_zval(*from TSRMLS_CC);
return size;
}
static size_t calc_property_info(zend_property_info * from TSRMLS_DC)
{
size_t size = 0;
ADDSIZE(size, sizeof(zend_property_info));
size += calc_string(from->name, from->name_length + 1 TSRMLS_CC);
#ifdef INCLUDE_DOC_COMMENTS
if (from->doc_comment != NULL) {
size += calc_string(from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
#endif
return size;
}
/* Calculate the size of an HashTable */
static size_t calc_hash_int(HashTable * source, Bucket * start,
calc_bucket_t calc_bucket TSRMLS_DC)
{
Bucket *p;
size_t size = 0;
if (source->nNumOfElements > 0) {
ADDSIZE(size, source->nTableSize * sizeof(Bucket *));
p = start;
while (p) {
#ifdef ZEND_ENGINE_2_4
// if (!IS_INTERNED(p->arKey) && p->nKeyLength) {
ADDSIZE(size, sizeof(Bucket));
ADDSIZE(size, p->nKeyLength);
// }
#else
ADDSIZE(size, offsetof(Bucket, arKey) + p->nKeyLength);
#endif
size += calc_bucket(p->pData TSRMLS_CC);
p = p->pListNext;
}
}
return size;
}
size_t calc_zval(zval *zv TSRMLS_DC)
{
size_t size = 0;
switch (EA_ZV_TYPE_P(zv)) {
case IS_CONSTANT:
case IS_OBJECT: /* object should have been serialized before storing them */
case IS_STRING:
size += calc_string(Z_STRVAL_P(zv), Z_STRLEN_P(zv) + 1 TSRMLS_CC);
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
if (Z_ARRVAL_P(zv) != NULL && Z_ARRVAL_P(zv) != &EG(symbol_table)) {
ADDSIZE(size, sizeof(HashTable));
size += calc_zval_hash(Z_ARRVAL_P(zv));
}
break;
case IS_RESOURCE:
DBG(ea_debug_error, ("[%d] EACCELERATOR can't cache resources\n", getpid()));
zend_bailout();
break;
default:
break;
}
return size;
}
/* Calculate the size of an op_array */
static size_t calc_op_array(zend_op_array * from TSRMLS_DC)
{
size_t size = 0;
if (from->type == ZEND_INTERNAL_FUNCTION) {
ADDSIZE(size, sizeof(zend_internal_function));
} else if (from->type == ZEND_USER_FUNCTION) {
ADDSIZE(size, sizeof(ea_op_array));
} else {
DBG(ea_debug_error, ("[%d] EACCELERATOR can't cache function \"%s\"\n", getpid(), from->function_name));
zend_bailout();
}
if (from->num_args > 0) {
zend_uint i;
ADDSIZE(size, from->num_args * sizeof(zend_arg_info));
for (i = 0; i < from->num_args; i++) {
if (from->arg_info[i].name) {
size += calc_string(from->arg_info[i].name, from->arg_info[i].name_len + 1 TSRMLS_CC);
}
if (from->arg_info[i].class_name) {
size += calc_string(from->arg_info[i].class_name, from->arg_info[i].class_name_len + 1 TSRMLS_CC);
}
}
}
if (from->function_name != NULL) {
size += calc_string(from->function_name, strlen(from->function_name) + 1 TSRMLS_CC);
}
if (from->scope != NULL) {
// HOESH: the same problem?
Bucket *q = CG(class_table)->pListHead;
while (q != NULL) {
if (*(zend_class_entry **) q->pData == from->scope) {
size += calc_string(q->arKey, q->nKeyLength TSRMLS_CC);
break;
}
q = q->pListNext;
}
}
if (from->type == ZEND_INTERNAL_FUNCTION) {
return size;
}
if (from->opcodes != NULL) {
ADDSIZE(size, from->last * sizeof(zend_op));
#ifndef ZEND_ENGINE_2_4
zend_op *opline, *end;
opline = from->opcodes;
end = opline + from->last;
for (; opline < end; opline++) {
if (opline->op1.op_type == IS_CONST) {
size += calc_zval(&opline->op1.u.constant TSRMLS_CC);
}
if (opline->op2.op_type == IS_CONST) {
size += calc_zval(&opline->op2.u.constant TSRMLS_CC);
}
}
#endif
}
#ifdef ZEND_ENGINE_2_4
if (from->literals != NULL) {
zend_literal *l, *end;
ADDSIZE(size, sizeof(zend_literal) * from->last_literal);
l = from->literals;
end = l + from->last_literal;
for (; l < end; l++) {
size += calc_zval(&l->constant TSRMLS_CC);
}
}
#endif
if (from->brk_cont_array != NULL) {
ADDSIZE(size, sizeof(zend_brk_cont_element) * from->last_brk_cont);
}
if (from->try_catch_array != NULL) {
ADDSIZE(size, sizeof(zend_try_catch_element) * from->last_try_catch);
}
if (from->static_variables != NULL) {
ADDSIZE(size, sizeof(HashTable));
size += calc_zval_hash(from->static_variables);
}
if (from->vars != NULL) {
int i;
ADDSIZE(size, sizeof(zend_compiled_variable) * from->last_var);
for (i = 0; i < from->last_var; i ++) {
size += calc_string(from->vars[i].name, from->vars[i].name_len+1 TSRMLS_CC);
}
}
if (from->filename != NULL) {
size += calc_string(from->filename, strlen(from->filename) + 1 TSRMLS_CC);
}
#ifdef INCLUDE_DOC_COMMENTS
if (from->doc_comment != NULL) {
size += calc_string(from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
#endif
return size;
}
/* Calculate the size of a class entry */
static size_t calc_class_entry(zend_class_entry * from TSRMLS_DC)
{
size_t size = 0;
if (from->type != ZEND_USER_CLASS) {
DBG(ea_debug_error, ("[%d] EACCELERATOR can't cache internal class \"%s\"\n", getpid(), from->name));
zend_bailout();
}
ADDSIZE(size, sizeof(ea_class_entry));
if (from->name != NULL) {
size += calc_string(from->name, from->name_length + 1 TSRMLS_CC);
}
if (from->parent != NULL && from->parent->name) {
size += calc_string(from->parent->name, from->parent->name_length + 1 TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2_4
if (from->info.user.filename != NULL) {
size += calc_string(from->info.user.filename, strlen(from->info.user.filename) + 1 TSRMLS_CC);
}
# ifdef INCLUDE_DOC_COMMENTS
if (from->info.user.doc_comment != NULL) {
size += calc_string(from->info.user.doc_comment, from->info.user.doc_comment_len + 1 TSRMLS_CC);
}
# endif
#else
if (from->filename != NULL) {
size += calc_string(from->filename, strlen(from->filename) + 1 TSRMLS_CC);
}
# ifdef INCLUDE_DOC_COMMENTS
if (from->doc_comment != NULL) {
size += calc_string(from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
# endif
#endif
size += calc_zval_hash(&from->constants_table);
size += calc_hash(&from->properties_info, (calc_bucket_t) calc_property_info);
#ifdef ZEND_ENGINE_2_4
if (from->default_properties_count) {
int i = 0;
ADDSIZE(size, sizeof(zval *) * from->default_properties_count);
for (i = 0; i < from->default_properties_count; i++) {
if (from->default_properties_table[i]) {
size += calc_zval_ptr(&from->default_properties_table[i] TSRMLS_CC);
}
}
}
if (from->default_static_members_count) {
int i = 0;
ADDSIZE(size, sizeof(zval *) * from->default_static_members_count);
for (i = 0; i < from->default_static_members_count; i++) {
if (from->default_static_members_table[i]) {
size += calc_zval_ptr(&from->default_static_members_table[i] TSRMLS_CC);
}
}
}
#else
size += calc_zval_hash(&from->default_properties);
size += calc_zval_hash(&from->default_static_members);
if ((from->static_members != NULL) && (from->static_members != &from->default_static_members)) {
ADDSIZE(size, sizeof(HashTable));
size += calc_zval_hash(from->static_members);
}
#endif
size += calc_hash(&from->function_table, (calc_bucket_t) calc_op_array);
return size;
}
/* Calculate the size of a cache entry with its given op_array and function and
class bucket */
size_t calc_size(char *key, zend_op_array * op_array, Bucket * f, Bucket * c TSRMLS_DC)
{
Bucket *b;
const char *x;
int len = strlen(key);
size_t size = 0;
zend_hash_init(&EAG(strings), 0, NULL, NULL, 0);
ADDSIZE(size, offsetof(ea_cache_entry, realfilename) + len + 1);
zend_hash_add(&EAG(strings), key, len + 1, &key, sizeof(char *), NULL);
b = c;
while (b != NULL) {
ADDSIZE(size, offsetof(ea_fc_entry, htabkey) + b->nKeyLength);
x = b->arKey;
zend_hash_add(&EAG(strings), b->arKey, b->nKeyLength, &x, sizeof(char *), NULL);
b = b->pListNext;
}
b = f;
while (b != NULL) {
ADDSIZE(size, offsetof(ea_fc_entry, htabkey) + b->nKeyLength);
x = b->arKey;
zend_hash_add(&EAG(strings), b->arKey, b->nKeyLength, &x, sizeof(char *), NULL);
b = b->pListNext;
}
while (c != NULL) {
size += calc_class_entry(*(zend_class_entry **) c->pData TSRMLS_CC);
c = c->pListNext;
}
while (f != NULL) {
size += calc_op_array((zend_op_array *) f->pData TSRMLS_CC);
f = f->pListNext;
}
size += calc_op_array(op_array TSRMLS_CC);
zend_hash_destroy(&EAG(strings));
return size;
}
// this macro returns the current position to place data and advances the pointer
// to the next positions and aligns it
#define ALLOCATE(at, len) (*at);\
(*at) += (len); \
EACCELERATOR_ALIGN((*at));
/* ugh, dirty */
#ifdef ZEND_ENGINE_2_4
static inline const char *store_string(char **at, const char *str, int len TSRMLS_DC)
#elif defined(ZEND_ENGINE_2_3)
static inline char *store_string(char **at, const char *str, int len TSRMLS_DC)
#else
static inline char *store_string(char **at, char *str, int len TSRMLS_DC)
#endif
{
char *p;
if (len > MAX_DUP_STR_LEN) {
p = ALLOCATE(at, len);
memcpy(p, str, len);
} else if (zend_hash_find(&EAG(strings), str, len , (void *) &p) == SUCCESS) {
p = *(char **) p;
} else {
p = ALLOCATE(at, len);
memcpy(p, str, len);
zend_hash_add(&EAG(strings), str, len, (void *) &p, sizeof(char *), NULL);
}
return p;
}
typedef void *(*store_bucket_t) (char **, void * TSRMLS_DC);
typedef void *(*check_bucket_t) (Bucket *, zend_class_entry *);
#define store_hash_ex(p, to, from, start, store_bucket, check_bucket, from_ce) \
store_hash_int(p, to, from, start, store_bucket, check_bucket, from_ce)
#define store_hash(p, to, from, store_bucket, check_bucket, from_ce) \
store_hash_ex(p, to, from, (from)->pListHead, store_bucket, check_bucket, from_ce)
#define store_zval_hash(p, to, from) \
store_hash(p, to, from, (store_bucket_t)store_zval_ptr, NULL, NULL)
#define store_zval_hash_ex(p, to, from, start) \
store_hash_ex(p, to, from, start, (store_bucket_t)store_zval_ptr, NULL)
static zval *store_zval_ptr(char **at, zval *from TSRMLS_DC)
{
zval *to = (zval *)ALLOCATE(at, sizeof(zval));
memcpy(to, from, sizeof(zval));
store_zval(at, to TSRMLS_CC);
return to;
}
static void store_hash_int(char **at, HashTable *target, HashTable *source,
Bucket *start, store_bucket_t copy_bucket,
check_bucket_t check_bucket,
zend_class_entry *from_ce)
{
Bucket *p, *np, *prev_p;
int nIndex;
TSRMLS_FETCH();
assert(target != NULL);
assert(source != NULL);
memcpy(target, source, sizeof(HashTable));
if (source->nNumOfElements > 0) {
target->arBuckets = (Bucket **)ALLOCATE(at, target->nTableSize * sizeof(Bucket *));
memset(target->arBuckets, 0, target->nTableSize * sizeof(Bucket *));
target->pDestructor = NULL;
target->persistent = 1;
target->pInternalPointer = NULL;
target->pListHead = NULL;
target->pListTail = NULL;
p = start;
prev_p = NULL;
np = NULL;
while (p) {
/* If a check function has been defined, run it */
if (check_bucket) {
/* If the check function returns ZEND_HASH_APPLY_REMOVE, don't store this record, skip over it */
if(check_bucket(p, from_ce)) {
p = p->pListNext;
target->nNumOfElements--;
/* skip to next itteration */
continue;
}
}
#ifdef ZEND_ENGINE_2_4
// if (IS_INTERNED(p->arKey)) {
/* TODO */
// DBG(ea_debug_printf, (EA_DEBUG, "[%d] store_hash_int: storing interned arKey '%s'\n", getpid(), p->arKey));
// np = (Bucket *)ALLOCATE(at, sizeof(Bucket));
// memcpy(np, p, sizeof(Bucket));
// } else if (!p->nKeyLength) {
// DBG(ea_debug_printf, (EA_DEBUG, "[%d] store_hash_int: storing zero length arKey '%s'\n", getpid(), p->arKey));
// np = (Bucket *)ALLOCATE(at, sizeof(Bucket));
// memcpy(np, p, sizeof(Bucket));
// } else {
DBG(ea_debug_printf, (EA_DEBUG, "[%d] store_hash_int: storing regular arKey '%s'\n", getpid(), p->arKey));
np = (Bucket *)ALLOCATE(at, sizeof(Bucket));
memcpy(np, p, sizeof(Bucket));
np->arKey = (char *)ALLOCATE(at, p->nKeyLength);
memcpy((char*)np->arKey, p->arKey, p->nKeyLength);
// }
#else
np = (Bucket *)ALLOCATE(at, offsetof(Bucket, arKey) + p->nKeyLength);
memcpy(np, p, offsetof(Bucket, arKey) + p->nKeyLength);
#endif
nIndex = p->h % target->nTableSize;
if (target->arBuckets[nIndex]) {
np->pNext = target->arBuckets[nIndex];
np->pLast = NULL;
np->pNext->pLast = np;
} else {
np->pNext = NULL;
np->pLast = NULL;
}
target->arBuckets[nIndex] = np;
if (p->pDataPtr == NULL) {
np->pData = copy_bucket(at, p->pData TSRMLS_CC);
np->pDataPtr = NULL;
} else {
np->pDataPtr = copy_bucket(at, p->pDataPtr TSRMLS_CC);
np->pData = &np->pDataPtr;
}
np->pListLast = prev_p;
np->pListNext = NULL;
if (prev_p) {
prev_p->pListNext = np;
} else {
target->pListHead = np;
}
prev_p = np;
p = p->pListNext;
}
target->pListTail = np;
zend_hash_internal_pointer_reset(target);
}
}
void store_zval(char **at, zval *zv TSRMLS_DC)
{
switch (EA_ZV_TYPE_P(zv)) {
case IS_CONSTANT:
case IS_OBJECT: /* object should have been serialized before storing them */
case IS_STRING:
Z_STRVAL_P(zv) = (char*)store_string(at, Z_STRVAL_P(zv), Z_STRLEN_P(zv) + 1 TSRMLS_CC);
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
if (Z_ARRVAL_P(zv) != NULL && Z_ARRVAL_P(zv) != &EG(symbol_table)) {
HashTable *q;
q = (HashTable *)ALLOCATE(at, sizeof(HashTable));
store_zval_hash(at, q, Z_ARRVAL_P(zv));
Z_ARRVAL_P(zv) = q;
}
break;
default:
break;
}
}
static ea_op_array *store_op_array(char **at, zend_op_array * from TSRMLS_DC)
{
ea_op_array *to;
zend_op *opline;
zend_uint i;
DBG(ea_debug_pad, (EA_DEBUG TSRMLS_CC));
DBG(ea_debug_printf, (EA_DEBUG, "[%d] store_op_array: %s [scope=%s type=%x]\n",
getpid(), from->function_name ? from->function_name : "(top)",
from->scope ? from->scope->name : "NULL"
, from->type
));
if (from->type == ZEND_INTERNAL_FUNCTION) {
to = (ea_op_array *)ALLOCATE(at, sizeof(zend_internal_function));
} else if (from->type == ZEND_USER_FUNCTION) {
to = (ea_op_array *)ALLOCATE(at, sizeof(ea_op_array));
} else {
return NULL;
}
to->type = from->type;
to->num_args = from->num_args;
to->required_num_args = from->required_num_args;
if (from->num_args > 0) {
to->arg_info = (zend_arg_info *)ALLOCATE(at, from->num_args * sizeof(zend_arg_info));
memcpy(to->arg_info, from->arg_info, from->num_args * sizeof(zend_arg_info));
for (i = 0; i < from->num_args; i++) {
if (from->arg_info[i].name) {
to->arg_info[i].name = store_string(at, from->arg_info[i].name, from->arg_info[i].name_len + 1 TSRMLS_CC);
}
if (from->arg_info[i].class_name) {
to->arg_info[i].class_name = store_string(at, from->arg_info[i].class_name, from->arg_info[i].class_name_len + 1 TSRMLS_CC);
}
}
}
#ifndef ZEND_ENGINE_2_4
to->pass_rest_by_reference = from->pass_rest_by_reference;
#endif
if (from->function_name != NULL) {
to->function_name = store_string(at, from->function_name, strlen(from->function_name) + 1 TSRMLS_CC);
}
to->fn_flags = from->fn_flags;
to->scope_name = NULL;
to->scope_name_len = 0;
if (from->scope != NULL) {
Bucket *q = CG(class_table)->pListHead;
while (q != NULL) {
if (*(zend_class_entry **) q->pData == from->scope) {
to->scope_name = (char*)store_string(at, q->arKey, q->nKeyLength TSRMLS_CC);
to->scope_name_len = q->nKeyLength - 1;
DBG(ea_debug_pad, (EA_DEBUG TSRMLS_CC));
DBG(ea_debug_printf, (EA_DEBUG,
"[%d] find scope '%s' in CG(class_table) save hashkey '%s' [%08x] as to->scope_name\n",
getpid(), from->scope->name ? from->scope->name : "NULL", q->arKey, to->scope_name));
break;
}
q = q->pListNext;
}
if (to->scope_name == NULL) {
DBG(ea_debug_pad, (EA_DEBUG TSRMLS_CC));
DBG(ea_debug_printf, (EA_DEBUG,
"[%d] could not find scope '%s' in CG(class_table), saving it to NULL\n",
getpid(), from->scope->name ? from->scope->name : "NULL"));
}
}
if (from->type == ZEND_INTERNAL_FUNCTION) {
#ifndef ZEND_ENGINE_2_4
/* zend_internal_function also contains return_reference in ZE2 */
to->return_reference = from->return_reference;
#endif
return to;
}
to->last = from->last;
to->T = from->T;
#ifdef ZEND_ENGINE_2_3
to->this_var = from->this_var;
to->early_binding = from->early_binding;
#else
to->uses_this = from->uses_this;
#endif
#ifdef ZEND_ENGINE_2_4
if (from->literals != NULL) {
zend_literal *p, *q, *end;
to->literals = (zend_literal *)ALLOCATE(at, sizeof(zend_literal) * from->last_literal);
memcpy(to->literals, from->literals, sizeof(zend_literal) * from->last_literal);
q = from->literals;
p = to->literals;
end = p + from->last_literal;
while (p < end) {
*p = *q;
store_zval(at, &p->constant TSRMLS_CC);
p++;
q++;
}
} else {
to->literals = NULL;
}
to->last_literal = from->last_literal;
to->last_cache_slot = from->last_cache_slot;
#endif
#ifndef ZEND_ENGINE_2_4
to->return_reference = from->return_reference;
#endif
to->filename = from->filename;
if (from->opcodes != NULL) {
to->opcodes = (zend_op *)ALLOCATE(at, from->last * sizeof(zend_op));
memcpy(to->opcodes, from->opcodes, from->last * sizeof(zend_op));
for (i = 0; i < from->last; i++) {
opline = &(to->opcodes[i]);
#ifndef ZEND_ENGINE_2_4
if (opline->op1.op_type == IS_CONST) {
store_zval(at, &opline->op1.u.constant TSRMLS_CC);
}
if (opline->op2.op_type == IS_CONST) {
store_zval(at, &opline->op2.u.constant TSRMLS_CC);
}
#endif
switch (opline->opcode) {
#ifdef ZEND_GOTO
case ZEND_GOTO:
#endif
case ZEND_JMP:
#ifdef ZEND_ENGINE_2_4
opline->op1.jmp_addr = to->opcodes + (opline->op1.jmp_addr - from->opcodes);
#else
opline->op1.u.jmp_addr = to->opcodes + (opline->op1.u.jmp_addr - from->opcodes);
#endif
break;
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
#ifdef ZEND_JMP_SET
case ZEND_JMP_SET:
#endif
#ifdef ZEND_ENGINE_2_4
case ZEND_JMP_SET_VAR:
opline->op2.jmp_addr = to->opcodes + (opline->op2.jmp_addr - from->opcodes);
#else
opline->op2.u.jmp_addr = to->opcodes + (opline->op2.u.jmp_addr - from->opcodes);
#endif
break;
}
#ifdef ZEND_ENGINE_2_4
if (opline->op1_type == IS_CONST) {
to->opcodes[i].op1.literal = from->opcodes[i].op1.literal - from->literals + to->literals;
}
if (opline->op2_type == IS_CONST) {
to->opcodes[i].op2.literal = from->opcodes[i].op2.literal - from->literals + to->literals;
}
#endif
}
}
to->last_try_catch = from->last_try_catch;
if (from->try_catch_array != NULL) {
to->try_catch_array = (zend_try_catch_element *)ALLOCATE(at, sizeof(zend_try_catch_element) * from->last_try_catch);
memcpy(to->try_catch_array, from->try_catch_array, sizeof(zend_try_catch_element) * from->last_try_catch);
}
to->last_brk_cont = from->last_brk_cont;
if (from->brk_cont_array != NULL) {
to->brk_cont_array = (zend_brk_cont_element *)ALLOCATE(at, sizeof(zend_brk_cont_element) * from->last_brk_cont);
memcpy(to->brk_cont_array, from->brk_cont_array, sizeof(zend_brk_cont_element) * from->last_brk_cont);
}
if (from->static_variables != NULL) {
to->static_variables = (HashTable *)ALLOCATE(at, sizeof(HashTable));
store_zval_hash(at, to->static_variables, from->static_variables);
} else {
to->static_variables = NULL;
}
if (from->vars != NULL) {
int i;
to->last_var = from->last_var;
to->vars = (zend_compiled_variable*)ALLOCATE(at, sizeof(zend_compiled_variable) * from->last_var);
memcpy(to->vars, from->vars, sizeof(zend_compiled_variable) * from->last_var);
for (i = 0; i < from->last_var; i++) {
to->vars[i].name = store_string(at, from->vars[i].name, from->vars[i].name_len + 1 TSRMLS_CC);
}
} else {
to->last_var = 0;
to->vars = NULL;
}
to->line_start = from->line_start;
to->line_end = from->line_end;
#ifdef INCLUDE_DOC_COMMENTS
to->doc_comment_len = from->doc_comment_len;
if (from->doc_comment != NULL) {
to->doc_comment = store_string(at, from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
#endif
if (from->filename != NULL) {
to->filename = store_string(at, from->filename, strlen(from->filename) + 1 TSRMLS_CC);
}
return to;
}
static zend_property_info *store_property_info(char **at, zend_property_info * from TSRMLS_DC)
{
zend_property_info *to;
to = (zend_property_info *)ALLOCATE(at, sizeof(zend_property_info));
memcpy(to, from, sizeof(*from));
to->name = NULL;
if (from->name) {
to->name = store_string(at, from->name, from->name_length + 1 TSRMLS_CC);
}
#ifdef INCLUDE_DOC_COMMENTS
to->doc_comment_len = from->doc_comment_len;
if (from->doc_comment) {
to->doc_comment = store_string(at, from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
#else
to->doc_comment_len = 0;
to->doc_comment = NULL;
#endif
return to;
}
/*
* The following two functions handle access checking of properties (public/private/protected)
* and control proper inheritance during copying of the properties_info and (default_)static_members hashes
*
* Both functions return ZEND_HASH_APPLY_REMOVE if the property to be copied needs to be skipped, or
* ZEND_HASH_APPLY_KEEP if the property needs to be copied over into the cache.
*
* If the property is skipped due to access restrictions, or it needs inheritance of its value from the
* parent, the restore phase will take care of that.
*
* Most of the logic behind all this can be found in zend_compile.c, functions zend_do_inheritance and
* do_inherit_property_access_check
*/
static int store_property_access_check(Bucket * p, zend_class_entry * from)
{
zend_class_entry *parent = from->parent;
zend_property_info* child_info = (zend_property_info*)p->pData;
zend_property_info* parent_info = NULL;
#ifdef ZEND_ENGINE_2_2
return (child_info->ce != from);
#endif
if (parent && zend_hash_quick_find(&parent->properties_info, p->arKey, p->nKeyLength, p->h, (void **) &parent_info)==SUCCESS) {
if(parent_info->flags & ZEND_ACC_PRIVATE) {
return ZEND_HASH_APPLY_KEEP;
}
/* if public/private/protected mask differs: copy, else let zend_do_inheritance handle this */
if((parent_info->flags & ZEND_ACC_PPP_MASK) != (child_info->flags & ZEND_ACC_PPP_MASK)) {
return ZEND_HASH_APPLY_KEEP;
}
return ZEND_HASH_APPLY_REMOVE;
}
return ZEND_HASH_APPLY_KEEP;
}
#ifndef ZEND_ENGINE_2_4
static int store_default_property_access_check(Bucket * p, zend_class_entry * from)
{
zend_class_entry *parent = from->parent;
union {
zend_property_info *v;
void *ptr;
} pinfo;
union {
zval **v;
void *ptr;
} pprop, cprop;
cprop.v = p->pData;
pprop.v = NULL;
/* Check if this is a parent class. If so, copy unconditionally */
if (parent &&
zend_hash_quick_find(&parent->default_properties, p->arKey, p->nKeyLength, p->h, &pinfo.ptr)==SUCCESS) {
if ((pprop.v == cprop.v) && (*pprop.v == *cprop.v)) {
return ZEND_HASH_APPLY_REMOVE;
}
}
return ZEND_HASH_APPLY_KEEP;
}
static int store_static_member_access_check(Bucket * p, zend_class_entry * from_ce)
{
zend_class_entry *from = from_ce;
zend_class_entry *parent = from->parent;
union {
zend_property_info *v;
void *ptr;
} pinfo, cinfo;
union {
zval **v;
void *ptr;
} pprop, cprop;
char *mname, *cname = NULL;
cprop.v = p->pData;
/* Check if this is a parent class. If so, copy unconditionally */
if (parent) {
/* unpack the \0classname\0membername\0 style property name to seperate vars */
# ifdef ZEND_ENGINE_2_2
zend_unmangle_property_name(p->arKey, p->nKeyLength-1, &cname, &mname);
# else
zend_unmangle_property_name(p->arKey, &cname, &mname);
# endif
/* lookup the member's info in parent and child */
if((zend_hash_find(&parent->properties_info, mname, strlen(mname)+1, &pinfo.ptr) == SUCCESS) &&
(zend_hash_find(&from->properties_info, mname, strlen(mname)+1, &cinfo.ptr) == SUCCESS)) {
/* If the static member points to the same value in parent and child, remove for proper inheritance during restore */
if(zend_hash_quick_find(&parent->default_static_members, p->arKey, p->nKeyLength, p->h, &pprop.ptr) == SUCCESS) {
if(*pprop.v == *cprop.v) {
return ZEND_HASH_APPLY_REMOVE;
}
}
}
}
return ZEND_HASH_APPLY_KEEP;
}
#endif /* ZEND_ENGINE_2_4 */
/*
* This function makes sure that functions/methods that are not in the scope of the current
* class being stored, do not get copied to the function_table hash. This makes sure they
* get properly inherited on restore by zend_do_inheritance
*
* If we dont do this, it will result in broken inheritance, problems with final methods
* (e.g. "Cannot override final method") and the like.
*/
static int store_function_inheritance_check(Bucket * p, zend_class_entry * from_ce)
{
zend_class_entry *from = from_ce;
zend_function *zf = p->pData;
if (zf->common.scope == from) {
return ZEND_HASH_APPLY_KEEP;
}
return ZEND_HASH_APPLY_REMOVE;
}
static ea_class_entry *store_class_entry(char **at, zend_class_entry * from TSRMLS_DC)
{
ea_class_entry *to;
unsigned int i;
to = (ea_class_entry *)ALLOCATE(at, sizeof(ea_class_entry));
memset(to, 0, sizeof(ea_class_entry));
to->type = from->type;
to->name = NULL;
to->name_length = from->name_length;
to->parent = NULL;
DBG(ea_debug_pad, (EA_DEBUG TSRMLS_CC));
DBG(ea_debug_printf, (EA_DEBUG, "[%d] store_class_entry: %s parent was '%s'\n",
getpid(), from->name ? from->name : "(top)",
from->parent ? from->parent->name : "NULL"));
#ifdef DEBUG
EAG(xpad)++;
#endif
if (from->name != NULL) {
to->name = store_string(at, from->name, from->name_length + 1 TSRMLS_CC);
}
if (from->parent != NULL && from->parent->name) {
to->parent = (char*)store_string(at, from->parent->name, from->parent->name_length + 1 TSRMLS_CC);
}
to->ce_flags = from->ce_flags;
/*
* Scan the interfaces looking for the first one which isn't 0
* This is the first inherited interface and should not be counted in the stored object
*/
for (i = 0 ; i < from->num_interfaces ; i++) {
if (from->interfaces[i] != 0) {
break;
}
}
to->num_interfaces = i;
/*
* hrak: no need to really store the interfaces since these get populated
* at/after restore by zend_do_inheritance and ZEND_ADD_INTERFACE
*/
#ifdef ZEND_ENGINE_2_4
to->line_start = from->info.user.line_start;
to->line_end = from->info.user.line_end;
# ifdef INCLUDE_DOC_COMMENTS
to->doc_comment_len = from->info.user.doc_comment_len;
# endif
if (from->info.user.filename != NULL) {
to->filename = store_string(at, from->info.user.filename, strlen(from->info.user.filename) + 1 TSRMLS_CC);
}
# ifdef INCLUDE_DOC_COMMENTS
if (from->info.user.doc_comment != NULL) {
to->doc_comment = store_string(at, from->info.user.doc_comment, from->info.user.doc_comment_len + 1 TSRMLS_CC);
}
# endif
#else
to->line_start = from->line_start;
to->line_end = from->line_end;
# ifdef INCLUDE_DOC_COMMENTS
to->doc_comment_len = from->doc_comment_len;
# endif
if (from->filename != NULL) {
to->filename = store_string(at, from->filename, strlen(from->filename) + 1 TSRMLS_CC);
}
# ifdef INCLUDE_DOC_COMMENTS
if (from->doc_comment != NULL) {
to->doc_comment = store_string(at, from->doc_comment, from->doc_comment_len + 1 TSRMLS_CC);
}
# endif
#endif
store_zval_hash(at, &to->constants_table, &from->constants_table);
/* Store default_properties */
#ifdef ZEND_ENGINE_2_4