-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_compile.c
2483 lines (2117 loc) · 80.7 KB
/
apc_compile.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
/*
+----------------------------------------------------------------------+
| APC |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
| Arun C. Murthy <[email protected]> |
| Gopal Vijayaraghavan <[email protected]> |
+----------------------------------------------------------------------+
This software was contributed to PHP by Community Connect Inc. in 2002
and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
Future revisions and derivatives of this source code must acknowledge
Community Connect Inc. as the original contributor of this module by
leaving this note intact in the source code.
All other licensing and usage conditions are those of the PHP Group.
*/
/* $Id: apc_compile.c 327148 2012-08-16 14:11:18Z laruence $ */
#include "apc_compile.h"
#include "apc_globals.h"
#include "apc_zend.h"
#include "apc_php.h"
#include "apc_string.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_smart_str.h"
typedef void* (*ht_copy_fun_t)(void*, void*, apc_context_t* TSRMLS_DC);
//typedef void (*ht_free_fun_t)(void*, apc_context_t*);
typedef int (*ht_check_copy_fun_t)(Bucket*, va_list);
typedef void (*ht_fixup_fun_t)(Bucket*, zend_class_entry*, zend_class_entry*);
#define CHECK(p) { if ((p) == NULL) return NULL; }
enum {
APC_FREE_HASHTABLE_FUNCS,
APC_FREE_HASHTABLE_PROPS,
APC_FREE_HASHTABLE_STATIC_PROPS,
};
/* {{{ internal function declarations */
static zend_function* my_bitwise_copy_function(zend_function*, zend_function*, apc_context_t* TSRMLS_DC);
/*
* The "copy" functions perform deep-copies on a particular data structure
* (passed as the second argument). They also optionally allocate space for
* the destination data structure if the first argument is null.
*/
static zval** my_copy_zval_ptr(zval**, const zval**, apc_context_t* TSRMLS_DC);
static zval* my_copy_zval(zval*, const zval*, apc_context_t* TSRMLS_DC);
#ifndef ZEND_ENGINE_2_4
static znode* my_copy_znode(znode*, znode*, apc_context_t* TSRMLS_DC);
#endif
static zend_op* my_copy_zend_op(zend_op*, zend_op*, apc_context_t* TSRMLS_DC);
static zend_function* my_copy_function(zend_function*, zend_function*, apc_context_t* TSRMLS_DC);
static zend_function_entry* my_copy_function_entry(zend_function_entry*, const zend_function_entry*, apc_context_t* TSRMLS_DC);
static zend_class_entry* my_copy_class_entry(zend_class_entry*, zend_class_entry*, apc_context_t* TSRMLS_DC);
static HashTable* my_copy_hashtable_ex(HashTable*, HashTable* TSRMLS_DC, ht_copy_fun_t, int, apc_context_t*, ht_check_copy_fun_t, ...);
#define my_copy_hashtable( dst, src, copy_fn, holds_ptr, ctxt) \
my_copy_hashtable_ex(dst, src TSRMLS_CC, copy_fn, holds_ptr, ctxt, NULL)
static HashTable* my_copy_static_variables(zend_op_array* src, apc_context_t* TSRMLS_DC);
static zend_property_info* my_copy_property_info(zend_property_info* dst, zend_property_info* src, apc_context_t* TSRMLS_DC);
static zend_arg_info* my_copy_arg_info_array(zend_arg_info*, const zend_arg_info*, uint, apc_context_t* TSRMLS_DC);
static zend_arg_info* my_copy_arg_info(zend_arg_info*, const zend_arg_info*, apc_context_t* TSRMLS_DC);
/*
* The "fixup" functions need for ZEND_ENGINE_2
*/
static void my_fixup_function( Bucket *p, zend_class_entry *src, zend_class_entry *dst );
static void my_fixup_hashtable( HashTable *ht, ht_fixup_fun_t fixup, zend_class_entry *src, zend_class_entry *dst );
/* my_fixup_function_for_execution is the same as my_fixup_function
* but named differently for clarity
*/
#define my_fixup_function_for_execution my_fixup_function
#ifdef ZEND_ENGINE_2_2
static void my_fixup_property_info( Bucket *p, zend_class_entry *src, zend_class_entry *dst );
#define my_fixup_property_info_for_execution my_fixup_property_info
#endif
/*
* These functions return "1" if the member/function is
* defined/overridden in the 'current' class and not inherited.
*/
static int my_check_copy_function(Bucket* src, va_list args);
static int my_check_copy_property_info(Bucket* src, va_list args);
#ifndef ZEND_ENGINE_2_4
static int my_check_copy_default_property(Bucket* p, va_list args);
static int my_check_copy_static_member(Bucket* src, va_list args);
#endif
static int my_check_copy_constant(Bucket* src, va_list args);
/* }}} */
/* {{{ apc php serializers */
int APC_SERIALIZER_NAME(php) (APC_SERIALIZER_ARGS)
{
smart_str strbuf = {0};
php_serialize_data_t var_hash;
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(&strbuf, (zval**)&value, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
if(strbuf.c) {
*buf = (unsigned char*)strbuf.c;
*buf_len = strbuf.len;
smart_str_0(&strbuf);
return 1;
}
return 0;
}
int APC_UNSERIALIZER_NAME(php) (APC_UNSERIALIZER_ARGS)
{
const unsigned char *tmp = buf;
php_unserialize_data_t var_hash;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if(!php_var_unserialize(value, &tmp, buf + buf_len, &var_hash TSRMLS_CC)) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
zval_dtor(*value);
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Error at offset %ld of %ld bytes", (long)(tmp - buf), (long)buf_len);
(*value)->type = IS_NULL;
return 0;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
return 1;
}
/* }}} */
/* {{{ check_op_array_integrity */
#if 0
static void check_op_array_integrity(zend_op_array* src)
{
int i, j;
/* These sorts of checks really aren't particularly effective, but they
* can provide a welcome sanity check when debugging. Just don't enable
* for production use! */
assert(src->refcount != NULL);
assert(src->opcodes != NULL);
assert(src->last > 0);
for (i = 0; i < src->last; i++) {
zend_op* op = &src->opcodes[i];
znode* nodes[] = { &op->result, &op->op1, &op->op2 };
for (j = 0; j < 3; j++) {
assert(nodes[j]->op_type == IS_CONST ||
nodes[j]->op_type == IS_VAR ||
nodes[j]->op_type == IS_TMP_VAR ||
nodes[j]->op_type == IS_UNUSED);
if (nodes[j]->op_type == IS_CONST) {
int type = nodes[j]->u.constant.type;
assert(type == IS_RESOURCE ||
type == IS_BOOL ||
type == IS_LONG ||
type == IS_DOUBLE ||
type == IS_NULL ||
type == IS_CONSTANT ||
type == IS_STRING ||
type == FLAG_IS_BC ||
type == IS_ARRAY ||
type == IS_CONSTANT_ARRAY ||
type == IS_OBJECT);
}
}
}
}
#endif
/* }}} */
/* {{{ my_bitwise_copy_function */
static zend_function* my_bitwise_copy_function(zend_function* dst, zend_function* src, apc_context_t* ctxt TSRMLS_DC)
{
apc_pool* pool = ctxt->pool;
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_function*) apc_pool_alloc(pool, sizeof(src[0])));
}
/* We only need to do a bitwise copy */
memcpy(dst, src, sizeof(src[0]));
return dst;
}
/* }}} */
/* {{{ my_copy_zval_ptr */
static zval** my_copy_zval_ptr(zval** dst, const zval** src, apc_context_t* ctxt TSRMLS_DC)
{
zval* dst_new;
apc_pool* pool = ctxt->pool;
int usegc = (ctxt->copy == APC_COPY_OUT_OPCODE) || (ctxt->copy == APC_COPY_OUT_USER);
assert(src != NULL);
if (!dst) {
CHECK(dst = (zval**) apc_pool_alloc(pool, sizeof(zval*)));
}
if(usegc) {
ALLOC_ZVAL(dst[0]);
CHECK(dst[0]);
} else {
CHECK((dst[0] = (zval*) apc_pool_alloc(pool, sizeof(zval))));
}
CHECK((dst_new = my_copy_zval(*dst, *src, ctxt TSRMLS_CC)));
if(dst_new != *dst) {
if(usegc) {
FREE_ZVAL(dst[0]);
}
*dst = dst_new;
}
return dst;
}
/* }}} */
/* {{{ my_serialize_object */
static zval* my_serialize_object(zval* dst, const zval* src, apc_context_t* ctxt TSRMLS_DC)
{
smart_str buf = {0};
apc_pool* pool = ctxt->pool;
apc_serialize_t serialize = APC_SERIALIZER_NAME(php);
void *config = NULL;
if(APCG(serializer)) { /* TODO: move to ctxt */
serialize = APCG(serializer)->serialize;
config = APCG(serializer)->config;
}
if(serialize((unsigned char**)&buf.c, &buf.len, src, config TSRMLS_CC)) {
dst->type = src->type & ~IS_CONSTANT_INDEX;
dst->value.str.len = buf.len;
CHECK(dst->value.str.val = apc_pmemcpy(buf.c, (buf.len + 1), pool TSRMLS_CC));
}
if(buf.c) smart_str_free(&buf);
return dst;
}
/* }}} */
/* {{{ my_unserialize_object */
static zval* my_unserialize_object(zval* dst, const zval* src, apc_context_t* ctxt TSRMLS_DC)
{
apc_unserialize_t unserialize = APC_UNSERIALIZER_NAME(php);
unsigned char *p = (unsigned char*)Z_STRVAL_P(src);
void *config = NULL;
if(APCG(serializer)) { /* TODO: move to ctxt */
unserialize = APCG(serializer)->unserialize;
config = APCG(serializer)->config;
}
if(unserialize(&dst, p, Z_STRLEN_P(src), config TSRMLS_CC)) {
return dst;
} else {
zval_dtor(dst);
dst->type = IS_NULL;
}
return dst;
}
/* }}} */
/* {{{ apc_string_pmemcpy */
static char *apc_string_pmemcpy(char *str, size_t len, apc_pool* pool TSRMLS_DC)
{
#ifdef ZEND_ENGINE_2_4
#ifndef ZTS
if (pool->type != APC_UNPOOL) {
char * ret = (char*)apc_new_interned_string((const char*)str, len TSRMLS_CC);
if (ret) {
return ret;
}
}
#endif
#endif
return apc_pmemcpy(str, len, pool TSRMLS_CC);
}
/* }}} */
/* {{{ my_copy_zval */
static APC_HOTSPOT zval* my_copy_zval(zval* dst, const zval* src, apc_context_t* ctxt TSRMLS_DC)
{
zval **tmp;
apc_pool* pool = ctxt->pool;
assert(dst != NULL);
assert(src != NULL);
memcpy(dst, src, sizeof(src[0]));
if(APCG(copied_zvals).nTableSize) {
if(zend_hash_index_find(&APCG(copied_zvals), (ulong)src, (void**)&tmp) == SUCCESS) {
if(Z_ISREF_P((zval*)src)) {
Z_SET_ISREF_PP(tmp);
}
Z_ADDREF_PP(tmp);
return *tmp;
}
zend_hash_index_update(&APCG(copied_zvals), (ulong)src, (void**)&dst, sizeof(zval*), NULL);
}
if(ctxt->copy == APC_COPY_OUT_USER || ctxt->copy == APC_COPY_IN_USER) {
/* deep copies are refcount(1), but moved up for recursive
* arrays, which end up being add_ref'd during its copy. */
Z_SET_REFCOUNT_P(dst, 1);
Z_UNSET_ISREF_P(dst);
} else {
/* code uses refcount=2 for consts */
Z_SET_REFCOUNT_P(dst, Z_REFCOUNT_P((zval*)src));
Z_SET_ISREF_TO_P(dst, Z_ISREF_P((zval*)src));
}
switch (src->type & IS_CONSTANT_TYPE_MASK) {
case IS_RESOURCE:
case IS_BOOL:
case IS_LONG:
case IS_DOUBLE:
case IS_NULL:
break;
case IS_CONSTANT:
case IS_STRING:
if (src->value.str.val) {
CHECK(dst->value.str.val = apc_string_pmemcpy(src->value.str.val,
src->value.str.len+1,
pool TSRMLS_CC));
}
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
if(APCG(serializer) == NULL ||
ctxt->copy == APC_COPY_IN_OPCODE || ctxt->copy == APC_COPY_OUT_OPCODE) {
CHECK(dst->value.ht =
my_copy_hashtable(NULL,
src->value.ht,
(ht_copy_fun_t) my_copy_zval_ptr,
1,
ctxt));
break;
} else {
/* fall through to object case */
}
case IS_OBJECT:
dst->type = IS_NULL;
if(ctxt->copy == APC_COPY_IN_USER) {
dst = my_serialize_object(dst, src, ctxt TSRMLS_CC);
} else if(ctxt->copy == APC_COPY_OUT_USER) {
dst = my_unserialize_object(dst, src, ctxt TSRMLS_CC);
}
break;
#ifdef ZEND_ENGINE_2_4
case IS_CALLABLE:
/* XXX implement this */
assert(0);
break;
#endif
default:
assert(0);
}
return dst;
}
/* }}} */
#ifdef ZEND_ENGINE_2_4
/* {{{ my_copy_znode */
static void my_check_znode(zend_uchar op_type, apc_context_t* ctxt TSRMLS_DC)
{
assert(op_type == IS_CONST ||
op_type == IS_VAR ||
op_type == IS_CV ||
op_type == IS_TMP_VAR ||
op_type == IS_UNUSED);
}
/* }}} */
/* {{{ my_copy_zend_op */
static zend_op* my_copy_zend_op(zend_op* dst, zend_op* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(dst != NULL);
assert(src != NULL);
memcpy(dst, src, sizeof(src[0]));
my_check_znode(dst->result_type & ~EXT_TYPE_UNUSED, ctxt TSRMLS_CC);
my_check_znode(dst->op1_type, ctxt TSRMLS_CC);
/* ZEND_BIND_TRAITS opcode don't set the op2 at all, so ommit its check */
if (dst->opcode != ZEND_BIND_TRAITS) {
my_check_znode(dst->op2_type, ctxt TSRMLS_CC);
}
return dst;
}
/* }}} */
#else
/* {{{ my_copy_znode */
static znode* my_copy_znode(znode* dst, znode* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(dst != NULL);
assert(src != NULL);
memcpy(dst, src, sizeof(src[0]));
#ifdef IS_CV
assert(dst ->op_type == IS_CONST ||
dst ->op_type == IS_VAR ||
dst ->op_type == IS_CV ||
dst ->op_type == IS_TMP_VAR ||
dst ->op_type == IS_UNUSED);
#else
assert(dst ->op_type == IS_CONST ||
dst ->op_type == IS_VAR ||
dst ->op_type == IS_TMP_VAR ||
dst ->op_type == IS_UNUSED);
#endif
if (src->op_type == IS_CONST) {
if(!my_copy_zval(&dst->u.constant, &src->u.constant, ctxt TSRMLS_CC)) {
return NULL;
}
}
return dst;
}
/* }}} */
/* {{{ my_copy_zend_op */
static zend_op* my_copy_zend_op(zend_op* dst, zend_op* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(dst != NULL);
assert(src != NULL);
memcpy(dst, src, sizeof(src[0]));
CHECK(my_copy_znode(&dst->result, &src->result, ctxt TSRMLS_CC));
CHECK(my_copy_znode(&dst->op1, &src->op1, ctxt TSRMLS_CC));
CHECK(my_copy_znode(&dst->op2, &src->op2, ctxt TSRMLS_CC));
return dst;
}
/* }}} */
#endif
/* {{{ my_copy_function */
static zend_function* my_copy_function(zend_function* dst, zend_function* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(src != NULL);
CHECK(dst = my_bitwise_copy_function(dst, src, ctxt TSRMLS_CC));
switch (src->type) {
case ZEND_INTERNAL_FUNCTION:
case ZEND_OVERLOADED_FUNCTION:
/* shallow copy because op_array is internal */
dst->op_array = src->op_array;
break;
case ZEND_USER_FUNCTION:
case ZEND_EVAL_CODE:
CHECK(apc_copy_op_array(&dst->op_array,
&src->op_array,
ctxt TSRMLS_CC));
break;
default:
assert(0);
}
/*
* op_array bitwise copying overwrites what ever you modified
* before apc_copy_op_array - which is why this code is outside
* my_bitwise_copy_function.
*/
/* zend_do_inheritance will re-look this up, because the pointers
* in prototype are from a function table of another class. It just
* helps if that one is from EG(class_table).
*/
dst->common.prototype = NULL;
/* once a method is marked as ZEND_ACC_IMPLEMENTED_ABSTRACT then you
* have to carry around a prototype. Thankfully zend_do_inheritance
* sets this properly as well
*/
dst->common.fn_flags = src->common.fn_flags & (~ZEND_ACC_IMPLEMENTED_ABSTRACT);
return dst;
}
/* }}} */
/* {{{ my_copy_function_entry */
static zend_function_entry* my_copy_function_entry(zend_function_entry* dst, const zend_function_entry* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_function_entry*) apc_pool_alloc(ctxt->pool, sizeof(src[0])));
}
/* Start with a bitwise copy */
memcpy(dst, src, sizeof(src[0]));
dst->fname = NULL;
dst->arg_info = NULL;
if (src->fname) {
CHECK((dst->fname = apc_pstrdup(src->fname, ctxt->pool TSRMLS_CC)));
}
if (src->arg_info) {
CHECK((dst->arg_info = my_copy_arg_info_array(NULL,
src->arg_info,
src->num_args,
ctxt TSRMLS_CC)));
}
return dst;
}
/* }}} */
/* {{{ my_copy_property_info */
static zend_property_info* my_copy_property_info(zend_property_info* dst, zend_property_info* src, apc_context_t* ctxt TSRMLS_DC)
{
apc_pool* pool = ctxt->pool;
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_property_info*) apc_pool_alloc(pool, sizeof(*src)));
}
/* Start with a bitwise copy */
memcpy(dst, src, sizeof(*src));
dst->name = NULL;
#if defined(ZEND_ENGINE_2) && PHP_MINOR_VERSION > 0
dst->doc_comment = NULL;
#endif
if (src->name) {
/* private members are stored inside property_info as a mangled
* string of the form:
* \0<classname>\0<membername>\0
*/
CHECK((dst->name = apc_string_pmemcpy((char *)src->name, src->name_length+1, pool TSRMLS_CC)));
}
#if defined(ZEND_ENGINE_2) && PHP_MINOR_VERSION > 0
if (src->doc_comment) {
CHECK((dst->doc_comment = apc_pmemcpy(src->doc_comment, (src->doc_comment_len + 1), pool TSRMLS_CC)));
}
#endif
return dst;
}
/* }}} */
/* {{{ my_copy_property_info_for_execution */
static zend_property_info* my_copy_property_info_for_execution(zend_property_info* dst, zend_property_info* src, apc_context_t* ctxt TSRMLS_DC)
{
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_property_info*) apc_pool_alloc(ctxt->pool, sizeof(*src)));
}
/* We need only a shallow copy */
memcpy(dst, src, sizeof(*src));
return dst;
}
/* }}} */
/* {{{ my_copy_arg_info_array */
static zend_arg_info* my_copy_arg_info_array(zend_arg_info* dst, const zend_arg_info* src, uint num_args, apc_context_t* ctxt TSRMLS_DC)
{
uint i = 0;
if (!dst) {
CHECK(dst = (zend_arg_info*) apc_pool_alloc(ctxt->pool, (sizeof(*src) * num_args)));
}
/* Start with a bitwise copy */
memcpy(dst, src, sizeof(*src)*num_args);
for(i=0; i < num_args; i++) {
CHECK((my_copy_arg_info( &dst[i], &src[i], ctxt TSRMLS_CC)));
}
return dst;
}
/* }}} */
/* {{{ my_copy_arg_info */
static zend_arg_info* my_copy_arg_info(zend_arg_info* dst, const zend_arg_info* src, apc_context_t* ctxt TSRMLS_DC)
{
apc_pool* pool = ctxt->pool;
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_arg_info*) apc_pool_alloc(pool, sizeof(*src)));
}
/* Start with a bitwise copy */
memcpy(dst, src, sizeof(*src));
dst->name = NULL;
dst->class_name = NULL;
if (src->name) {
CHECK((dst->name = apc_string_pmemcpy((char *) src->name, src->name_len+1, pool TSRMLS_CC)));
}
if (src->class_name) {
CHECK((dst->class_name = apc_string_pmemcpy((char *) src->class_name, src->class_name_len+1, pool TSRMLS_CC)));
}
return dst;
}
/* }}} */
/* {{{ apc_copy_class_entry */
zend_class_entry* apc_copy_class_entry(zend_class_entry* dst, zend_class_entry* src, apc_context_t* ctxt TSRMLS_DC)
{
return my_copy_class_entry(dst, src, ctxt TSRMLS_CC);
}
/* }}} */
/* {{{ my_copy_class_entry */
static zend_class_entry* my_copy_class_entry(zend_class_entry* dst, zend_class_entry* src, apc_context_t* ctxt TSRMLS_DC)
{
int i = 0;
apc_pool* pool = ctxt->pool;
assert(src != NULL);
if (!dst) {
CHECK(dst = (zend_class_entry*) apc_pool_alloc(pool, sizeof(*src)));
}
/* Start with a bitwise copy */
memcpy(dst, src, sizeof(*src));
dst->name = NULL;
memset(&dst->function_table, 0, sizeof(dst->function_table));
ZEND_CE_DOC_COMMENT(dst) = NULL;
ZEND_CE_FILENAME(dst) = NULL;
memset(&dst->properties_info, 0, sizeof(dst->properties_info));
memset(&dst->constants_table, 0, sizeof(dst->constants_table));
if (src->name) {
CHECK((dst->name = apc_pstrdup(src->name, pool TSRMLS_CC)));
}
if(!(my_copy_hashtable_ex(&dst->function_table,
&src->function_table TSRMLS_CC,
(ht_copy_fun_t) my_copy_function,
0,
ctxt,
(ht_check_copy_fun_t) my_check_copy_function,
src))) {
return NULL;
}
/* the interfaces are populated at runtime using ADD_INTERFACE */
dst->interfaces = NULL;
/* the current count includes inherited interfaces as well,
the real dynamic ones are the first <n> which are zero'd
out in zend_do_end_class_declaration */
for(i = 0 ; (uint)i < src->num_interfaces ; i++) {
if(src->interfaces[i])
{
dst->num_interfaces = i;
break;
}
}
/* these will either be set inside my_fixup_hashtable or
* they will be copied out from parent inside zend_do_inheritance
*/
dst->parent = NULL;
dst->constructor = NULL;
dst->destructor = NULL;
dst->clone = NULL;
dst->__get = NULL;
dst->__set = NULL;
dst->__unset = NULL;
dst->__isset = NULL;
dst->__call = NULL;
#ifdef ZEND_ENGINE_2_2
dst->__tostring = NULL;
#endif
#ifdef ZEND_ENGINE_2_3
dst->__callstatic = NULL;
#endif
#ifdef ZEND_ENGINE_2_4
if (src->trait_aliases) {
int num_trait_aliases = 0;
/* get how much trait aliases we've got */
while (src->trait_aliases[num_trait_aliases]) {num_trait_aliases++;}
/* copy all the trait aliases */
CHECK(dst->trait_aliases =
(zend_trait_alias **) apc_pool_alloc(pool, sizeof(zend_trait_alias *)*(num_trait_aliases+1)));
i = 0;
while (src->trait_aliases[i] && i < num_trait_aliases) {
dst->trait_aliases[i] = apc_copy_trait_alias(NULL, src->trait_aliases[i], ctxt TSRMLS_CC);
i++;
}
dst->trait_aliases[i] = NULL;
}
if (src->trait_precedences) {
int num_trait_precedences = 0;
/* get how much trait precedences we've got */
while (src->trait_precedences[num_trait_precedences]) {num_trait_precedences++;}
/* copy all the trait precedences */
CHECK(dst->trait_precedences =
(zend_trait_alias **) apc_pool_alloc(pool, sizeof(zend_trait_alias *)*(num_trait_precedences+1)));
i = 0;
while (src->trait_precedences[i] && i < num_trait_precedences) {
dst->trait_precedences[i] = apc_copy_trait_precedence(NULL, src->trait_precedences[i], ctxt TSRMLS_CC);
i++;
}
dst->trait_precedences[i] = NULL;
}
#endif
/* unset function proxies */
dst->serialize_func = NULL;
dst->unserialize_func = NULL;
my_fixup_hashtable(&dst->function_table, (ht_fixup_fun_t)my_fixup_function, src, dst);
#ifdef ZEND_ENGINE_2_4
dst->default_properties_count = src->default_properties_count;
if (src->default_properties_count) {
CHECK(dst->default_properties_table = (zval**) apc_pool_alloc(pool, (sizeof(zval*) * src->default_properties_count)));
for (i = 0; i < src->default_properties_count; i++) {
if (src->default_properties_table[i]) {
my_copy_zval_ptr(&dst->default_properties_table[i], (const zval**)&src->default_properties_table[i], ctxt TSRMLS_CC);
} else {
dst->default_properties_table[i] = NULL;
}
}
} else {
dst->default_properties_table = NULL;
}
#else
memset(&dst->default_properties, 0, sizeof(dst->default_properties));
CHECK((my_copy_hashtable_ex(&dst->default_properties,
&src->default_properties TSRMLS_CC,
(ht_copy_fun_t) my_copy_zval_ptr,
1,
ctxt,
(ht_check_copy_fun_t) my_check_copy_default_property,
src)));
#endif
CHECK((my_copy_hashtable_ex(&dst->properties_info,
&src->properties_info TSRMLS_CC,
(ht_copy_fun_t) my_copy_property_info,
0,
ctxt,
(ht_check_copy_fun_t) my_check_copy_property_info,
src)));
#ifdef ZEND_ENGINE_2_2
/* php5.2 introduced a scope attribute for property info */
my_fixup_hashtable(&dst->properties_info, (ht_fixup_fun_t)my_fixup_property_info_for_execution, src, dst);
#endif
#ifdef ZEND_ENGINE_2_4
dst->default_static_members_count = src->default_static_members_count;
if (src->default_static_members_count) {
CHECK(dst->default_static_members_table = (zval**) apc_pool_alloc(pool, (sizeof(zval*) * src->default_static_members_count)));
for (i = 0; i < src->default_static_members_count; i++) {
if (src->default_static_members_table[i]) {
my_copy_zval_ptr(&dst->default_static_members_table[i], (const zval**)&src->default_static_members_table[i], ctxt TSRMLS_CC);
} else {
dst->default_static_members_table[i] = NULL;
}
}
} else {
dst->default_static_members_table = NULL;
}
dst->static_members_table = dst->default_static_members_table;
#else
memset(&dst->default_static_members, 0, sizeof(dst->default_static_members));
dst->static_members = NULL;
CHECK(my_copy_hashtable_ex(&dst->default_static_members,
&src->default_static_members TSRMLS_CC,
(ht_copy_fun_t) my_copy_zval_ptr,
1,
ctxt,
(ht_check_copy_fun_t) my_check_copy_static_member,
src,
&src->default_static_members));
if(src->static_members != &src->default_static_members)
{
CHECK((dst->static_members = my_copy_hashtable_ex(NULL,
src->static_members TSRMLS_CC,
(ht_copy_fun_t) my_copy_zval_ptr,
1,
ctxt,
(ht_check_copy_fun_t) my_check_copy_static_member,
src,
src->static_members)));
}
else
{
dst->static_members = &dst->default_static_members;
}
#endif
CHECK((my_copy_hashtable_ex(&dst->constants_table,
&src->constants_table TSRMLS_CC,
(ht_copy_fun_t) my_copy_zval_ptr,
1,
ctxt,
(ht_check_copy_fun_t) my_check_copy_constant,
src)));
if (src->type == ZEND_USER_CLASS && ZEND_CE_DOC_COMMENT(src)) {
CHECK(ZEND_CE_DOC_COMMENT(dst) =
apc_pmemcpy(ZEND_CE_DOC_COMMENT(src), (ZEND_CE_DOC_COMMENT_LEN(src) + 1), pool TSRMLS_CC));
}
if (src->type == ZEND_INTERNAL_CLASS && ZEND_CE_BUILTIN_FUNCTIONS(src)) {
int n;
for (n = 0; src->type == ZEND_INTERNAL_CLASS && ZEND_CE_BUILTIN_FUNCTIONS(src)[n].fname != NULL; n++) {}
CHECK((ZEND_CE_BUILTIN_FUNCTIONS(dst) =
(zend_function_entry*) apc_pool_alloc(pool, ((n + 1) * sizeof(zend_function_entry)))));
for (i = 0; i < n; i++) {
CHECK(my_copy_function_entry((zend_function_entry*)(&ZEND_CE_BUILTIN_FUNCTIONS(dst)[i]),
&ZEND_CE_BUILTIN_FUNCTIONS(src)[i],
ctxt TSRMLS_CC));
}
*(char**)&(ZEND_CE_BUILTIN_FUNCTIONS(dst)[n].fname) = NULL;
}
if (src->type == ZEND_USER_CLASS && ZEND_CE_FILENAME(src)) {
CHECK((ZEND_CE_FILENAME(dst) = apc_pstrdup(ZEND_CE_FILENAME(src), pool TSRMLS_CC)));
}
return dst;
}
/* }}} */
/* {{{ my_free_hashtable */
static void my_free_hashtable(HashTable* ht, int type TSRMLS_DC)
{
Bucket *curr = NULL;
Bucket *prev = NULL;
assert(ht != NULL);
prev = ht->pListHead;
while (prev != NULL) {
curr = prev;
prev = prev->pListNext;
#ifdef ZEND_ENGINE_2_4
if (!IS_INTERNED(curr->arKey)) {
apc_php_free((char*)curr->arKey TSRMLS_CC);
}
#else
apc_php_free(curr->arKey TSRMLS_CC);
#endif
switch (type) {
case APC_FREE_HASHTABLE_FUNCS:
do {
#if 0
/* XXX need to check where else the method is referenced,
the refcount leak is still there */
zend_function *fn;
fn = (zend_function *)curr->pData;
apc_free_op_array_after_execution(&fn->op_array TSRMLS_CC);
#endif
efree(curr->pData);
} while (0);
break;
case APC_FREE_HASHTABLE_PROPS:
case APC_FREE_HASHTABLE_STATIC_PROPS:
efree(curr->pData);
break;
}
/* XXX check if this must be ignored for any class method */
if (type != APC_FREE_HASHTABLE_FUNCS) {
apc_php_free(curr TSRMLS_CC);
}
}
apc_php_free(ht->arBuckets TSRMLS_CC);
}
/* }}} */
/* {{{ my_copy_hashtable_ex */
static APC_HOTSPOT HashTable* my_copy_hashtable_ex(HashTable* dst,
HashTable* src TSRMLS_DC,
ht_copy_fun_t copy_fn,
int holds_ptrs,
apc_context_t* ctxt,
ht_check_copy_fun_t check_fn,
...)
{
Bucket* curr = NULL;
Bucket* prev = NULL;
Bucket* newp = NULL;
int first = 1;
apc_pool* pool = ctxt->pool;
assert(src != NULL);
if (!dst) {
CHECK(dst = (HashTable*) apc_pool_alloc(pool, sizeof(src[0])));
}
memcpy(dst, src, sizeof(src[0]));
/* allocate buckets for the new hashtable */
CHECK((dst->arBuckets = apc_pool_alloc(pool, (dst->nTableSize * sizeof(Bucket*)))));
memset(dst->arBuckets, 0, dst->nTableSize * sizeof(Bucket*));
dst->pInternalPointer = NULL;
dst->pListHead = NULL;
for (curr = src->pListHead; curr != NULL; curr = curr->pListNext) {
int n = curr->h % dst->nTableSize;
if(check_fn) {
va_list args;
va_start(args, check_fn);
/* Call the check_fn to see if the current bucket
* needs to be copied out
*/
if(!check_fn(curr, args)) {
dst->nNumOfElements--;
va_end(args);
continue;
}
va_end(args);
}
/* create a copy of the bucket 'curr' */
#ifdef ZEND_ENGINE_2_4
if (!curr->nKeyLength) {
CHECK((newp = (Bucket*) apc_pmemcpy(curr, sizeof(Bucket), pool TSRMLS_CC)));
} else if (IS_INTERNED(curr->arKey)) {
CHECK((newp = (Bucket*) apc_pmemcpy(curr, sizeof(Bucket), pool TSRMLS_CC)));
#ifndef ZTS
} else if (pool->type != APC_UNPOOL) {