-
Notifications
You must be signed in to change notification settings - Fork 1
/
wincache_zvcache.c
2374 lines (1901 loc) · 66.2 KB
/
wincache_zvcache.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
/*
+----------------------------------------------------------------------------------------------+
| Windows Cache for PHP |
+----------------------------------------------------------------------------------------------+
| Copyright (c) 2009, Microsoft Corporation. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, are |
| permitted provided that the following conditions are met: |
| - Redistributions of source code must retain the above copyright notice, this list of |
| conditions and the following disclaimer. |
| - Redistributions in binary form must reproduce the above copyright notice, this list of |
| conditions and the following disclaimer in the documentation and/or other materials provided |
| with the distribution. |
| - Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| used to endorse or promote products derived from this software without specific prior written|
| permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE|
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| OF THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------------------------+
| Module: wincache_zvcache.c |
+----------------------------------------------------------------------------------------------+
| Author: Kanwaljeet Singla <[email protected]> |
| Revised: Eric Stenson <[email protected]> |
+----------------------------------------------------------------------------------------------+
*/
#include "precomp.h"
/*
* NOTES:
*
* copyin_* - Copies zend types into memory cache by performing a deep copy.
* In the process, all pointers are converted to offsets from the beginning of
* the memory cache segment. This is done because the user cache is allocated
* in shared memory at arbitrary addresses in each process.
*
* copyout_* - Creates new instances of zend types that were previously copied
* into the memory cache. This involves converting offsets into pointers which
* are valid within the current process. All objects should be marked as "copy-
* on-write", such that the data that lives in the memory cache is not modified.
*
*/
/*
* zvcache uses pointers as HashTable index values. Therefore, we need
* zend_long's to be at least as big as size_t.
*/
C_ASSERT(SIZEOF_SIZE_T <= SIZEOF_ZEND_LONG);
#define PER_RUN_SCAVENGE_COUNT 16
#ifndef WINCACHE_MAX_ZVKEY_LENGTH
#define WINCACHE_MAX_ZVKEY_LENGTH 0xFFFF
#endif /* WINCACHE_MAX_ZVKEY_LENGTH */
#define ZMALLOC(pcopy, size) (pcopy->fnmalloc(pcopy->palloc, pcopy->hoffset, size))
#define ZREALLOC(pcopy, addr, size) (pcopy->fnrealloc(pcopy->palloc, pcopy->hoffset, addr, size))
#define ZSTRDUP(pcopy, pstr) (pcopy->fnstrdup(pcopy->palloc, pcopy->hoffset, pstr))
#define ZFREE(pcopy, addr) (pcopy->fnfree(pcopy->palloc, pcopy->hoffset, addr))
#define ZOFFSET(pcopy, pbuffer) ((pbuffer == NULL) ? 0 : (((char *)pbuffer) - (pcopy)->pbaseadr))
#define ZVALUE(pcopy, offset) ((offset == 0) ? NULL : ((pcopy)->pbaseadr + (offset)))
#define ZVCACHE_VALUE(p, o) ((zvcache_value *)alloc_get_cachevalue(p, o))
#define ZVALUE_HT_GET_DATA_ADDR(pcopy, ht) \
((char*)(ZVALUE((pcopy),(size_t)(ht)->arData)) - HT_HASH_SIZE((ht)->nTableMask))
static int copyin_memory(zvcopy_context * pcopy, HashTable * phtable, void * src, size_t size, void **ppdest, zend_uchar *pallocated);
static int copyin_zval(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zval * poriginal, zval ** ppcopied);
static int copyin_hashtable(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, HashTable * poriginal, HashTable ** ppcopied);
static int copyin_string(zvcopy_context * pcopy, HashTable *phxlate, zend_string * orig_string, zend_string ** new_string);
static int copyin_reference(zvcache_context * pcache, zvcopy_context * pcopy, HashTable *phtable, zend_reference * orig_ref, zend_reference ** new_ref);
static int copyout_zval(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zval * pcached, zval ** ppcopied);
static int copyout_hashtable(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, HashTable * pcached, HashTable ** ppcopied);
static int copyout_reference(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zend_reference * pcached, zend_reference ** ppnew_ref);
static int find_zvcache_entry(zvcache_context * pcache, const char * key, unsigned int index, zvcache_value ** ppvalue);
static int create_zvcache_data(zvcache_context * pcache, const char * key, zval * pzval, unsigned int ttl, zvcache_value ** ppvalue);
static void destroy_zvcache_data(zvcache_context * pcache, zvcache_value * pvalue);
static void add_zvcache_entry(zvcache_context * pcache, unsigned int index, zvcache_value * pvalue);
static void remove_zvcache_entry(zvcache_context * pcache, unsigned int index, zvcache_value * pvalue);
static void run_zvcache_scavenger(zvcache_context * pcache);
/* Globals */
static unsigned short gzvcacheid = 1;
#pragma warning( push )
#pragma warning( disable : 4146 ) /* we do negative indexing with unsigneds */
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] = {HT_INVALID_IDX, HT_INVALID_IDX};
#pragma warning( pop )
/* Private functions */
static int copyin_memory(zvcopy_context * pcopy, HashTable * phtable, void * src, size_t size, void **ppdest, zend_uchar *pallocated)
{
int result = NONFATAL;
void * pdest = NULL;
_ASSERT(pallocated != NULL);
_ASSERT(ppdest != NULL);
_ASSERT(*ppdest == NULL);
*pallocated = 0;
if(phtable != NULL && phtable->nTableSize)
{
/* Check if memory is already copied */
if((pdest = zend_hash_index_find_ptr(phtable, (zend_ulong)src)) != NULL)
{
*ppdest = pdest;
goto Finished;
}
}
/* Allocate cache memory & copy the original into the cache. */
pdest = (zend_string *)ZMALLOC(pcopy, size);
if (pdest == NULL)
{
result = pcopy->oomcode;
goto Finished;
}
*pallocated = 1;
pcopy->allocsize += size;
memcpy_s(pdest, size, src, size);
/* update the table */
if(phtable != NULL && phtable->nTableSize)
{
zend_hash_index_update_ptr(phtable, (zend_ulong)src, (void *)pdest);
}
*ppdest = pdest;
Finished:
return result;
}
static int copyin_zval(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zval * poriginal, zval ** ppcopied)
{
int result = NONFATAL;
zval * pnewzv = NULL;
zval * pfromht = NULL;
zend_uchar allocated = 0;
char * pbuffer = NULL;
size_t length = 0;
HashTable * phashtable = NULL;
zvcopy_context * phashcopy = NULL;
size_t offset = 0;
php_serialize_data_t serdata = {0};
smart_str smartstr = {0};
unsigned char isfirst = 0;
zend_string * pzstr = NULL;
zvcache_hashtable_pool_tracker * table_tracker = NULL;
zend_reference * pzref = NULL;
dprintverbose("start copyin_zval");
_ASSERT(pcache != NULL);
_ASSERT(pcopy != NULL);
_ASSERT(poriginal != NULL);
_ASSERT(ppcopied != NULL);
if (NULL == *ppcopied)
{
result = copyin_memory(pcopy, phtable, poriginal, sizeof(zval), &pnewzv, &allocated);
if (FAILED(result))
{
goto Finished;
}
if (!allocated)
{
*ppcopied = pnewzv;
goto Finished;
}
}
else
{
/* copy into an already allocated zval */
pnewzv = *ppcopied;
if (poriginal != pnewzv)
{
/* struct copy to pick up flags and such. Still need to fix up value pointer. */
*pnewzv = *poriginal;
}
}
switch(Z_TYPE_P(poriginal))
{
case IS_RESOURCE:
_ASSERT(FALSE);
result = FATAL_ZVCACHE_INVALID_ZVAL;
goto Finished;
case IS_UNDEF:
case IS_NULL:
case IS_TRUE:
case IS_FALSE:
case IS_LONG:
case IS_DOUBLE:
break;
case IS_STRING:
case IS_CONSTANT_AST:
result = copyin_string(pcopy, phtable, Z_STR_P(poriginal), &pzstr);
if (FAILED(result))
{
goto Finished;
}
/* Fix up the zval type flags */
if (Z_TYPE_P(poriginal) == IS_STRING) {
Z_TYPE_FLAGS_P(pnewzv) |= (IS_TYPE_REFCOUNTED);
}
else {
Z_TYPE_FLAGS_P(pnewzv) |= (Z_CONSTANT(poriginal) | IS_TYPE_REFCOUNTED);
}
/* Use offset in cached zval pointer */
Z_STR_P(pnewzv) = (zend_string *)ZOFFSET(pcopy, pzstr);
break;
case IS_ARRAY:
/* Initialize zvcopied hashtable for direct call to copyin_zval */
if(phtable == NULL)
{
phtable = WCG(zvcopied);
isfirst = 1;
zend_hash_init(phtable, 0, NULL, NULL, 0);
}
/*
* Because we need to remember the offset of the memory pool, all
* HashTables that are copied into shared memory will use the *ptr
* field, instead of the *arr field.
* The *ptr will point at a zvcache_hashtable_pool_tracker structure.
*/
table_tracker = (zvcache_hashtable_pool_tracker *)ZMALLOC(pcopy, sizeof(zvcache_hashtable_pool_tracker));
if (table_tracker == NULL)
{
result = pcopy->oomcode;
goto Finished;
}
pcopy->allocsize += sizeof(zvcache_hashtable_pool_tracker);
/* If pcopy is not set to use memory pool, use a different copy context */
if(pcopy->hoffset == 0)
{
_ASSERT(pcache->incopy == pcopy);
phashcopy = (zvcopy_context *)alloc_pemalloc(sizeof(zvcopy_context));
if(phashcopy == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
result = alloc_create_mpool(pcache->zvalloc, &offset);
if(FAILED(result))
{
goto Finished;
}
phashcopy->oomcode = FATAL_OUT_OF_SMEMORY;
phashcopy->palloc = pcache->zvalloc;
phashcopy->pbaseadr = pcache->zvmemaddr;
phashcopy->hoffset = offset;
phashcopy->allocsize = 0;
phashcopy->fnmalloc = alloc_ommalloc;
phashcopy->fnrealloc = alloc_omrealloc;
phashcopy->fnstrdup = alloc_omstrdup;
phashcopy->fnfree = alloc_omfree;
result = copyin_hashtable(pcache, phashcopy, phtable, Z_ARR_P(poriginal), &phashtable);
pcopy->allocsize += phashcopy->allocsize;
}
else
{
result = copyin_hashtable(pcache, pcopy, phtable, Z_ARR_P(poriginal), &phashtable);
}
if(isfirst)
{
_ASSERT(phtable != NULL);
zend_hash_destroy(phtable);
phtable->nTableSize = 0;
phtable = NULL;
}
if(FAILED(result))
{
goto Finished;
}
/* fix up the flags on the copied in zval */
Z_TYPE_INFO_P(pnewzv) = IS_ARRAY_EX;
/* Keep offset so that freeing shared memory is easy */
table_tracker->hoff = offset;
table_tracker->val = ZOFFSET(pcopy, phashtable);
phashtable = NULL;
Z_PTR_P(pnewzv) = (void *)ZOFFSET(pcopy, table_tracker);
table_tracker = NULL;
break;
case IS_REFERENCE:
result = copyin_reference(pcache, pcopy, phtable, Z_REF_P(poriginal), &pzref);
if(FAILED(result))
{
goto Finished;
}
Z_REF_P(pnewzv) = (zend_reference *)ZOFFSET(pcopy, pzref);
break;
case IS_OBJECT:
/* Serialize object and store in string */
PHP_VAR_SERIALIZE_INIT(serdata);
php_var_serialize(&smartstr, poriginal, &serdata);
PHP_VAR_SERIALIZE_DESTROY(serdata);
if(smartstr.s && ZSTR_LEN(smartstr.s))
{
size_t size = _ZSTR_STRUCT_SIZE(ZSTR_LEN(smartstr.s));
zend_string * pdest = NULL;
pdest = (zend_string *)ZMALLOC(pcopy, size);
if (pdest == NULL)
{
result = pcopy->oomcode;
goto Finished;
}
/* Allocate the zend_string & copy the original into the cache. */
pcopy->allocsize += size;
memcpy_s(pdest, size, smartstr.s, size);
/* Use Offset in copied-in zval */
Z_PTR_P(pnewzv) = (zend_string *)ZOFFSET(pcopy, pdest);
smart_str_free(&smartstr);
}
else
{
ZVAL_NULL(pnewzv);
}
break;
default:
result = FATAL_ZVCACHE_INVALID_ZVAL;
goto Finished;
}
*ppcopied = pnewzv;
_ASSERT(SUCCEEDED(result));
Finished:
if(phashcopy != NULL)
{
alloc_pefree(phashcopy);
phashcopy = NULL;
}
if(FAILED(result))
{
dprintimportant("failure %d in copyin_zval", result);
if(pnewzv != NULL)
{
if(allocated == 1 && offset == 0)
{
ZFREE(pcopy, pnewzv);
pnewzv = NULL;
}
if(offset != 0)
{
alloc_free_mpool(pcache->zvalloc, offset);
offset = 0;
}
}
}
dprintverbose("end copyin_zval");
return result;
}
static int copyout_zval(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zval * pcached, zval ** ppcopied)
{
int result = NONFATAL;
zval * pnewzv = NULL;
zval * pfromht = NULL;
zend_string * ptmp_str = NULL;
int allocated = 0;
char * pbuffer = NULL;
HashTable * phashtable = NULL;
php_unserialize_data_t serdata = {0};
unsigned char isfirst = 0;
zend_uchar added_to_phtable = 0;
zvcache_hashtable_pool_tracker * table_tracker = NULL;
zend_reference * pref = NULL;
dprintverbose("start copyout_zval");
_ASSERT(pcache != NULL);
_ASSERT(pcopy != NULL);
_ASSERT(pcached != NULL);
_ASSERT(ppcopied != NULL);
if(phtable != NULL && phtable->nTableSize)
{
/* Check if zval is already copied */
if((pfromht = zend_hash_index_find_ptr(phtable, (zend_ulong)pcached)) != NULL)
{
if (Z_REFCOUNTED_P(pfromht)) { Z_ADDREF_P(pfromht); }
*ppcopied = pfromht;
goto Finished;
}
}
/* Allocate memory as required */
if(*ppcopied == NULL)
{
pnewzv = (zval *)ZMALLOC(pcopy, sizeof(zval));
if(pnewzv == NULL)
{
result = pcopy->oomcode;
goto Finished;
}
allocated = 1;
}
else
{
pnewzv = *ppcopied;
}
_ASSERT(pnewzv != NULL);
if(phtable != NULL && phtable->nTableSize)
{
zend_hash_index_update_ptr(phtable, (zend_ulong)pcached, (void *)pnewzv);
added_to_phtable = 1;
}
/*
* struct copy the zval - this picks up the flags and type info from the
* cached zval.
* However, the value field is still an offset into the cache. Need to
* convert to an actual pointer before doing anything else.
*/
*pnewzv = *pcached;
switch(Z_TYPE_P(pcached))
{
case IS_RESOURCE:
/* We don't support caching of zend_resource types */
_ASSERT(FALSE);
result = FATAL_ZVCACHE_INVALID_ZVAL;
goto Finished;
case IS_TRUE:
case IS_FALSE:
case IS_UNDEF:
case IS_LONG:
case IS_DOUBLE:
case IS_NULL:
/* Nothing to do */
break;
case IS_STRING:
case IS_CONSTANT_AST:
/* create a new, non-persistent string from the cached copy */
ptmp_str = (zend_string *)ZVALUE(pcache->incopy, (size_t)Z_STR_P(pcached));
Z_STR_P(pnewzv) = zend_string_alloc(ZSTR_LEN(ptmp_str), 0);
memcpy(ZSTR_VAL(Z_STR_P(pnewzv)), ZSTR_VAL(ptmp_str), ZSTR_LEN(ptmp_str)+1);
break;
case IS_ARRAY:
/* Initialize zvcopied HashTable for first call to copyout_zval */
if(phtable == NULL)
{
phtable = WCG(zvcopied);
isfirst = 1;
zend_hash_init(WCG(zvcopied), 0, NULL, NULL, 0);
}
table_tracker = (zvcache_hashtable_pool_tracker *)ZVALUE(pcache->incopy, (size_t)Z_PTR_P(pcached));
result = copyout_hashtable(pcache, pcopy, phtable, (HashTable *)ZVALUE(pcache->incopy, table_tracker->val), &phashtable);
if(isfirst)
{
_ASSERT(phtable != NULL);
zend_hash_destroy(phtable);
phtable->nTableSize = 0;
phtable = NULL;
}
if(FAILED(result))
{
goto Finished;
}
Z_ARR_P(pnewzv) = phashtable;
phashtable = NULL;
break;
case IS_REFERENCE:
result = copyout_reference(pcache, pcopy, phtable, (zend_reference *)ZVALUE(pcache->incopy, (size_t)Z_REF_P(pcached)), &pref);
if(FAILED(result))
{
goto Finished;
}
Z_REF_P(pnewzv) = pref;
break;
case IS_OBJECT:
/* Deserialize stored data to produce object */
ptmp_str = (zend_string *)ZVALUE(pcache->incopy, (size_t)Z_PTR_P(pcached));
pbuffer = ZSTR_VAL(ptmp_str);
PHP_VAR_UNSERIALIZE_INIT(serdata);
if(!php_var_unserialize(pnewzv, (const unsigned char **)&pbuffer, (const unsigned char *)pbuffer + ZSTR_LEN(ptmp_str), &serdata))
{
/* Destroy zval and set return value to null */
dprintimportant("Failed to unserialize cached object: %s", ZSTR_VAL(ptmp_str));
ZVAL_NULL(pnewzv);
}
PHP_VAR_UNSERIALIZE_DESTROY(serdata);
break;
default:
result = FATAL_ZVCACHE_INVALID_ZVAL;
goto Finished;
}
*ppcopied = pnewzv;
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in copyout_zval", result);
if (added_to_phtable == 1)
{
zend_hash_index_del(phtable, (zend_ulong)pcached);
}
if(pbuffer != NULL)
{
ZFREE(pcopy, pbuffer);
pbuffer = NULL;
}
if(pnewzv != NULL)
{
if(allocated == 1)
{
ZFREE(pcopy, pnewzv);
pnewzv = NULL;
}
}
}
dprintverbose("end copyout_zval");
return result;
}
static int copyin_hashtable(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, HashTable * poriginal, HashTable ** ppcopied)
{
int result = NONFATAL;
uint32_t idx;
uint32_t nIndex;
Bucket * p = NULL;
zend_string * pzstr = NULL;
zend_uchar allocated = 0;
HashTable * pnew_table = NULL;
char * ptemp = NULL;
size_t temp_size = 0;
zval * pzval = NULL;
_ASSERT(ppcopied);
_ASSERT(*ppcopied == NULL);
dprintverbose("begin copyin_hashtable");
/* allocate a hashtable & copy over existing table stuff */
result = copyin_memory(pcopy, phtable, poriginal, sizeof(zend_array), &pnew_table, &allocated);
if (FAILED(result))
{
goto Finished;
}
*ppcopied = pnew_table;
if (allocated == 0)
{
/* already copied */
goto Finished;
}
dprintverbose(" copyin_hashtable: allocate hashtable");
/* Clear out the destructor, since we're taking ownership of the table */
pnew_table->pDestructor = NULL;
dprintverbose(" copyin_hashtable: clear destructor");
/* fix up the HashTable flags since we're persisting it */
GC_SET_REFCOUNT(pnew_table,2);
//GC_REF_SET_INFO(pnew_table,0);
//GC_INFO_SET_ADDRESS(pnew_table, 0);
GC_DEL_FLAGS(pnew_table, IS_ARRAY_IMMUTABLE);
pnew_table->u.flags &= ~IS_ARRAY_PERSISTENT;
dprintverbose(" copyin_hashtable: GC Flags");
/* Uninitalized */
if (!(pnew_table->u.flags & HASH_FLAG_INITIALIZED)) {
HT_SET_DATA_ADDR(pnew_table, 0);
goto Finished;
}
dprintverbose(" copyin_hashtable: Make sure initialized");
/* Packed (a.k.a. simple array) */
if (pnew_table->u.flags & HASH_FLAG_PACKED) {
dprintverbose(" copyin_hashtable: packed simple array");
result = copyin_memory(pcopy, phtable, HT_GET_DATA_ADDR(poriginal), HT_USED_SIZE(poriginal), &ptemp, &allocated);
if (FAILED(result))
{
goto Finished;
}
HT_SET_DATA_ADDR(pnew_table, ptemp);
ptemp = NULL;
}
#pragma warning( push )
#pragma warning( disable : 4018 ) /* yeah, we do signed/unsigned comparisons */
/* Sparsely Populated: Need to compress while copying in */
else if (poriginal->nNumUsed < -(int32_t)poriginal->nTableMask / 2) {
dprintverbose(" copyin_hashtable: sparsely populated");
/* compact table */
Bucket *old_buckets = poriginal->arData;
int32_t hash_size = -(int32_t)poriginal->nTableMask;
if (poriginal->nNumUsed <= HT_MIN_SIZE) {
hash_size = HT_MIN_SIZE;
} else {
while (hash_size >> 1 > poriginal->nNumUsed) {
hash_size >>= 1;
}
}
dprintverbose(" copyin_hashtable: hashsize done");
pnew_table->nTableMask = -hash_size;
temp_size = (hash_size * sizeof(uint32_t)) + (poriginal->nNumUsed * sizeof(Bucket));
ptemp = ZMALLOC(pcopy, temp_size); dprintverbose(" copyin_hashtable: zmalloc ptemp");
if (!ptemp)
{
result = pcopy->oomcode;
goto Finished;
}
pcopy->allocsize += temp_size;
HT_SET_DATA_ADDR(pnew_table, ptemp); dprintverbose(" copyin_hashtable: HT SET DATA ADDR");
ptemp = NULL; dprintverbose(" copyin_hashtable: ptemp nulled");
WC_HT_HASH_RESET(pnew_table); dprintverbose(" copyin_hashtable: HT Hash Reset");
memcpy(pnew_table->arData, old_buckets, poriginal->nNumUsed * sizeof(Bucket));
dprintverbose(" copyin_hashtable: memcpy done");
for (idx = 0; idx < poriginal->nNumUsed; idx++) {
p = pnew_table->arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
/* fix up the index table */
nIndex = ((uint32_t)p->h) | pnew_table->nTableMask;
Z_NEXT(p->val) = HT_HASH(pnew_table, nIndex);
HT_HASH(pnew_table, nIndex) = HT_IDX_TO_HASH(idx);
}
}
#pragma warning( pop )
/* Appropriately sized: Copy the whole thing in */
else {
result = copyin_memory(pcopy, phtable, HT_GET_DATA_ADDR(poriginal), HT_USED_SIZE(poriginal), &ptemp, &allocated);
HT_SET_DATA_ADDR(pnew_table, ptemp);
}
dprintverbose(" copyin_hashtable: copyin memory done");
/* Copy in Keys and Buckets */
for (idx = 0; idx < poriginal->nNumUsed; idx++) {
p = pnew_table->arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
/* persist bucket and key */
if (p->key) {
/* If we have a string key, we're no longer using static keys, since
* we have to copy the string into shared memory, thus it won't be
* interned.
*/
pnew_table->u.flags &= ~HASH_FLAG_STATIC_KEYS;
result = copyin_string(pcopy, phtable, p->key, &pzstr);
if (FAILED(result))
{
goto Finished;
}
/* Copy over the key string's hash from the existing hash value in
* the bucket.
*/
ZSTR_H(pzstr) = p->h;
/* Use Offset in copied-in zval */
p->key = (zend_string *)ZOFFSET(pcopy, pzstr);
pzstr = NULL;
}
/* persist the data itself */
pzval = &p->val;
result = copyin_zval(pcache, pcopy, phtable, pzval, &pzval);
if (FAILED(result))
{
goto Finished;
}
}
dprintverbose(" copyin_hashtable: for loop keys done");
/* ensure pointer is translated to cache memory offset */
pnew_table->arData = (Bucket *)ZOFFSET(pcopy, pnew_table->arData);
*ppcopied = pnew_table;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in copyin_hashtable", result);
}
dprintverbose("end copyin_hashtable");
return result;
}
static int copyout_hashtable(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, HashTable * pcached, HashTable ** ppcopied)
{
int result = NONFATAL;
uint32_t idx;
Bucket * p = NULL;
HashTable * pnew_table = NULL;
zend_uchar allocated = 0;
char * ptemp = NULL;
zval * pnewzv = NULL;
size_t tmp_size;
char * pdata_temp;
zend_string * ptmp_str = NULL;
dprintverbose("start copyout_hashtable");
_ASSERT(pcache != NULL);
_ASSERT(pcopy != NULL);
_ASSERT(pcached != NULL);
_ASSERT(ppcopied != NULL);
/* check table to see if we've already copied this item out*/
if(phtable != NULL && phtable->nTableSize)
{
if((pnew_table = zend_hash_index_find_ptr(phtable, (zend_ulong)pcached)) != NULL)
{
GC_ADDREF(pnew_table);
*ppcopied = pnew_table;
goto Finished;
}
}
/* Allocate memory for HashTable as required */
if(*ppcopied == NULL)
{
pnew_table = (HashTable *)ZMALLOC(pcopy, sizeof(HashTable));
if(pnew_table == NULL)
{
result = pcopy->oomcode;
goto Finished;
}
*ppcopied = pnew_table;
allocated = 1;
}
else
{
pnew_table = *ppcopied;
}
/* update the table */
if(phtable != NULL && phtable->nTableSize)
{
zend_hash_index_update_ptr(phtable, (zend_ulong)pcached, (void *)pnew_table);
}
/* initalize from cached entry */
*pnew_table = *pcached;
/* Uninitalized */
if (!(pnew_table->u.flags & HASH_FLAG_INITIALIZED)) {
HT_SET_DATA_ADDR(pnew_table, &uninitialized_bucket);
goto Finished;
}
tmp_size = HT_USED_SIZE(pcached);
/* Allocate memory for buckets */
ptemp = ZMALLOC(pcopy, tmp_size);
if (!ptemp)
{
result = pcopy->oomcode;
goto Finished;
}
pdata_temp = ZVALUE_HT_GET_DATA_ADDR(pcache->incopy, pcached);
memcpy(ptemp, pdata_temp, tmp_size);
HT_SET_DATA_ADDR(pnew_table, ptemp);
/* Copy out Keys and Buckets */
for (idx = 0; idx < pcached->nNumUsed; idx++) {
p = pnew_table->arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
/* copy out key */
if (p->key) {
/* Use pointer to copied-in string */
ptmp_str = (zend_string *)ZVALUE(pcache->incopy, (size_t)p->key);
p->key = zend_string_alloc(ZSTR_LEN(ptmp_str), 0);
memcpy(ZSTR_VAL(p->key), ZSTR_VAL(ptmp_str), ZSTR_LEN(ptmp_str)+1);
GC_ADDREF(p->key);
}
/* copy out the data itself */
pnewzv = &p->val;
result = copyout_zval(pcache, pcopy, phtable, pnewzv, &pnewzv);
if (FAILED(result)) {
goto Finished;
}
if (Z_REFCOUNTED_P(pnewzv)) { Z_ADDREF_P(pnewzv); }
}
/* After copying out all the values, reset the destructor */
pnew_table->pDestructor = ZVAL_PTR_DTOR;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in copyout_hashtable", result);
if(pnew_table != NULL)
{
if(allocated != 0)
{
ZFREE(pcopy, pnew_table);
pnew_table = NULL;
}
}
if (ptemp)
{
ZFREE(pcopy, ptemp);
ptemp = NULL;
}
}
dprintverbose("end copyout_hashtable");
return result;
}
static int copyin_string(zvcopy_context * pcopy, HashTable *phtable, zend_string * orig_string, zend_string ** new_string)
{
int result = NONFATAL;
size_t size = 0;
zend_string * pzstr = NULL;
zend_uchar allocated = 0;
_ASSERT(orig_string != NULL);
_ASSERT(new_string != NULL);
_ASSERT(*new_string == NULL);
size = _ZSTR_STRUCT_SIZE(ZSTR_LEN(orig_string));
result = copyin_memory(pcopy, phtable, orig_string, size, &pzstr, &allocated);
if (FAILED(result))
{
goto Finished;
}
if (allocated)
{
*(((char *)pzstr) + size - 1) = 0;
zend_string_forget_hash_val(pzstr);
GC_DEL_FLAGS(pzstr,(IS_STR_INTERNED | IS_STR_PERMANENT | IS_STR_PERSISTENT));
}
*new_string = pzstr;
Finished:
return result;
}
static int copyin_reference(zvcache_context * pcache, zvcopy_context * pcopy, HashTable *phtable, zend_reference * orig_ref, zend_reference ** new_ref)
{
int result = NONFATAL;
size_t size = 0;
zend_reference * pzref = NULL;
zval * pnew_zval = NULL;
zend_uchar allocated = 0;
_ASSERT(orig_ref != NULL);
_ASSERT(new_ref != NULL);
_ASSERT(*new_ref == NULL);
size = sizeof(zend_reference);
result = copyin_memory(pcopy, phtable, orig_ref, size, &pzref, &allocated);
if (FAILED(result))
{
goto Finished;
}
if (allocated)
{
pnew_zval = &pzref->val;
result = copyin_zval(pcache, pcopy, phtable, pnew_zval, &pnew_zval);
GC_SET_REFCOUNT(pzref,2);
//GC_REF_SET_INFO(pzref,0);
}
*new_ref = pzref;
Finished:
return result;
}
static int copyout_reference(zvcache_context * pcache, zvcopy_context * pcopy, HashTable * phtable, zend_reference * pcached, zend_reference ** ppnew_ref)
{
int result = NONFATAL;
size_t size = 0;
zend_reference * pzref = NULL;
zval * pnew_zval = NULL;
_ASSERT(pcache != NULL);
_ASSERT(pcopy != NULL);
_ASSERT(pcached != NULL);
_ASSERT(ppnew_ref != NULL);
/* check table to see if we've already copied this item out*/
if(phtable != NULL && phtable->nTableSize)
{
if((pzref = zend_hash_index_find_ptr(phtable, (zend_ulong)pcached)) != NULL)
{
GC_ADDREF(pzref);
*ppnew_ref = pzref;
goto Finished;
}
}
/* Allocate memory as required */
if(*ppnew_ref == NULL)
{
pzref = (zend_reference *)ZMALLOC(pcopy, sizeof(zend_reference));
if(pzref == NULL)
{
result = pcopy->oomcode;
goto Finished;
}