forked from phadej/igbinary
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathigbinary.c
More file actions
3600 lines (3247 loc) · 120 KB
/
Copy pathigbinary.c
File metadata and controls
3600 lines (3247 loc) · 120 KB
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
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Oleg Grenrus <[email protected]> |
| See CREDITS for contributors |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef PHP_WIN32
# include "ig_win32.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "Zend/zend_alloc.h"
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"
#include "Zend/zend_compile.h" /* ZEND_ACC_NOT_SERIALIZABLE */
#include "ext/standard/info.h"
#include "ext/standard/php_var.h"
#if PHP_VERSION_ID >= 80100
#include "Zend/zend_enum.h"
#endif
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
# include "ext/session/php_session.h"
#endif
#include "ext/standard/php_incomplete_class.h"
#if PHP_VERSION_ID < 70400
#define zend_get_properties_for(struc, purpose) Z_OBJPROP_P((struc))
#define zend_release_properties(ht) do {} while (0)
#endif
#if PHP_VERSION_ID < 70300
#define zend_string_efree(s) zend_string_release((s))
#define GC_ADDREF(p) (++GC_REFCOUNT((p)))
#endif
#if defined(HAVE_APCU_SUPPORT)
# include "ext/apcu/apc_serializer.h"
#endif /* HAVE_APCU_SUPPORT */
#include "php_igbinary.h"
#include "igbinary.h"
#include "igbinary_macros.h"
#include <assert.h>
#include <ctype.h>
#ifndef PHP_WIN32
# include <inttypes.h>
# include <stdbool.h>
# include <stdint.h>
#endif
#include <stddef.h>
#include "hash.h"
#include "hash_ptr.h"
#include "zend_alloc.h"
#include "igbinary_zend_hash.h"
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
/** Session serializer function prototypes. */
PS_SERIALIZER_FUNCS(igbinary);
#endif /* HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION) */
#if defined(HAVE_APCU_SUPPORT)
/** Apc serializer function prototypes */
static int APC_SERIALIZER_NAME(igbinary) (APC_SERIALIZER_ARGS);
static int APC_UNSERIALIZER_NAME(igbinary) (APC_UNSERIALIZER_ARGS);
#endif
static zend_always_inline HashTable *HASH_OF_OBJECT(zval *p) {
ZEND_ASSERT(Z_TYPE_P(p) == IS_OBJECT);
return Z_OBJ_HT_P(p)->get_properties(
#if PHP_VERSION_ID >= 80000
Z_OBJ_P(p)
#else
p
#endif
);
}
#if PHP_VERSION_ID < 70300
#define zend_string_release_ex(s, persistent) zend_string_release((s))
static zend_always_inline void zval_ptr_dtor_str(zval *zval_ptr)
{
if (Z_REFCOUNTED_P(zval_ptr) && !Z_DELREF_P(zval_ptr)) {
ZEND_ASSERT(Z_TYPE_P(zval_ptr) == IS_STRING);
ZEND_ASSERT(!ZSTR_IS_INTERNED(Z_STR_P(zval_ptr)));
ZEND_ASSERT(!(GC_FLAGS(Z_STR_P(zval_ptr)) & IS_STR_PERSISTENT));
efree(Z_STR_P(zval_ptr));
}
}
#endif
#define RETURN_1_IF_NON_ZERO(cmd) \
if (UNEXPECTED((cmd) != 0)) { \
return 1; \
}
#ifdef ZEND_ACC_NOT_SERIALIZABLE
# define IGBINARY_IS_NOT_SERIALIZABLE(ce) UNEXPECTED((ce)->ce_flags & (ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_ANON_CLASS))
# define IGBINARY_IS_NOT_UNSERIALIZABLE(ce) IGBINARY_IS_NOT_SERIALIZABLE(ce)
#elif PHP_VERSION_ID >= 70400
# define IGBINARY_IS_NOT_SERIALIZABLE(ce) UNEXPECTED((ce)->serialize == zend_class_serialize_deny)
# define IGBINARY_IS_NOT_UNSERIALIZABLE(ce) UNEXPECTED((ce)->unserialize == zend_class_unserialize_deny)
#else
// Because '__serialize' is not available prior to 7.4, this check is redundant.
# define IGBINARY_IS_NOT_SERIALIZABLE(ce) (0)
# define IGBINARY_IS_NOT_UNSERIALIZABLE(ce) (0)
#endif
/* {{{ Types */
enum igbinary_type {
/* 00 */ igbinary_type_null, /**< Null. */
/* 01 */ igbinary_type_ref8, /**< Array reference. */
/* 02 */ igbinary_type_ref16, /**< Array reference. */
/* 03 */ igbinary_type_ref32, /**< Array reference. */
/* 04 */ igbinary_type_bool_false, /**< Boolean true. */
/* 05 */ igbinary_type_bool_true, /**< Boolean false. */
/* 06 */ igbinary_type_long8p, /**< Long 8bit positive. */
/* 07 */ igbinary_type_long8n, /**< Long 8bit negative. */
/* 08 */ igbinary_type_long16p, /**< Long 16bit positive. */
/* 09 */ igbinary_type_long16n, /**< Long 16bit negative. */
/* 0a */ igbinary_type_long32p, /**< Long 32bit positive. */
/* 0b */ igbinary_type_long32n, /**< Long 32bit negative. */
/* 0c */ igbinary_type_double, /**< Double. */
/* 0d */ igbinary_type_string_empty, /**< Empty string. */
/* 0e */ igbinary_type_string_id8, /**< String id. */
/* 0f */ igbinary_type_string_id16, /**< String id. */
/* 10 */ igbinary_type_string_id32, /**< String id. */
/* 11 */ igbinary_type_string8, /**< String. */
/* 12 */ igbinary_type_string16, /**< String. */
/* 13 */ igbinary_type_string32, /**< String. */
/* 14 */ igbinary_type_array8, /**< Array. */
/* 15 */ igbinary_type_array16, /**< Array. */
/* 16 */ igbinary_type_array32, /**< Array. */
/* 17 */ igbinary_type_object8, /**< Object. */
/* 18 */ igbinary_type_object16, /**< Object. */
/* 19 */ igbinary_type_object32, /**< Object. */
/* 1a */ igbinary_type_object_id8, /**< Object class name string id. */
/* 1b */ igbinary_type_object_id16, /**< Object class name string id. */
/* 1c */ igbinary_type_object_id32, /**< Object class name string id. */
/* 1d */ igbinary_type_object_ser8, /**< Object serialized data. */
/* 1e */ igbinary_type_object_ser16, /**< Object serialized data. */
/* 1f */ igbinary_type_object_ser32, /**< Object serialized data. */
/* 20 */ igbinary_type_long64p, /**< Long 64bit positive. */
/* 21 */ igbinary_type_long64n, /**< Long 64bit negative. */
/* 22 */ igbinary_type_objref8, /**< Object reference. */
/* 23 */ igbinary_type_objref16, /**< Object reference. */
/* 24 */ igbinary_type_objref32, /**< Object reference. */
/* 25 */ igbinary_type_ref, /**< Simple reference */
/* 26 */ igbinary_type_string64, /**< String larger than 4GB (originally, php strings had a limit of 32-bit lengths). */
/* 27 */ igbinary_type_enum_case, /**< PHP 8.1 Enum case. */
};
/* Defers calls to zval_ptr_dtor for values that are refcounted. */
struct deferred_dtor_tracker {
zval *zvals; /**< refcounted objects and arrays to call dtor on after unserializing. See i_zval_ptr_dtor */
size_t count; /**< count of refcounted in array for calls to dtor */
size_t capacity; /**< capacity of refcounted in array for calls to dtor */
};
/** Serializer data.
* @author Oleg Grenrus <[email protected]>
*/
struct igbinary_serialize_data {
uint8_t *buffer; /**< Buffer. */
size_t buffer_size; /**< Buffer size. */
size_t buffer_capacity; /**< Buffer capacity. */
bool scalar; /**< Serializing scalar. */
bool compact_strings; /**< Check for duplicate strings. */
struct hash_si strings; /**< Hash of already serialized strings. */
struct hash_si_ptr references; /**< Hash of already serialized potential references. (non-NULL uintptr_t => int32_t) */
uint32_t references_id; /**< Number of things that the unserializer might think are references. >= length of references */
uint32_t string_count; /**< Serialized string count, used for back referencing */
struct deferred_dtor_tracker deferred_dtor_tracker; /**< refcounted objects and arrays to call dtor on after serializing. See i_zval_ptr_dtor */
};
/*
Object {
reference {scalar, object, array, null} (convert to reference, share reference in zval_ref)
object {} (convert to zend_object, share zend_object* in reference)
array {} (convert to zend_array, share zend_array* in reference)
empty array {} (use ZVAL_EMPTY_ARRAY to create zvals)
}
*/
enum zval_ref_type {
IG_REF_IS_REFERENCE, // Points to zend_reference
IG_REF_IS_OBJECT, // Points to zend_object
IG_REF_IS_ARRAY, // Points to zend_array
#if PHP_VERSION_ID >= 70300
IG_REF_IS_EMPTY_ARRAY, // Use the macro ZVAL_EMPTY_ARRAY to create a pointer to the empty array with the correct type info flags.
#endif
};
struct igbinary_value_ref {
// We reuse temporary values for object properties that are references or arrays.
union {
zend_reference *reference;
zend_object *object;
zend_array *array;
} reference;
enum zval_ref_type type;
};
struct deferred_unserialize_call {
zval param; /* The array parameter passed to the __unserialize call */
zend_object *object; /* The object which has a deferred call to __unserialize that is going to get called. */
};
struct deferred_call {
union {
zend_object *wakeup;
#if PHP_VERSION_ID >= 70400
/* Currently, zvals are safe to relocate */
struct deferred_unserialize_call unserialize;
#endif
} data;
#if PHP_VERSION_ID >= 70400
zend_bool is_unserialize;
#endif
};
/** Unserializer data.
* @author Oleg Grenrus <[email protected]>
*/
struct igbinary_unserialize_data {
const uint8_t *buffer; /**< Buffer with bytes to unserialize. */
const uint8_t *buffer_end; /**< Buffer size. */
const uint8_t *buffer_ptr; /**< Current read offset. */
zend_string **strings; /**< Unserialized strings. */
size_t strings_count; /**< Unserialized string count. */
size_t strings_capacity; /**< Unserialized string array capacity. */
struct igbinary_value_ref *references; /**< Unserialized Arrays/Objects/References */
size_t references_count; /**< Unserialized array/objects count. */
size_t references_capacity; /**< Unserialized array/object array capacity. */
struct deferred_call *deferred; /**< objects&data for calls to __unserialize/__wakeup */
size_t deferred_capacity; /**< capacity of objects in array for calls to __unserialize/__wakeup */
uint32_t deferred_count; /**< count of objects in array for calls to __unserialize/__wakeup. NOTE: Current php releases including 8.1 limit the total number of objects to a 32-bit integer. */
zend_bool deferred_finished; /**< whether the deferred calls were performed */
struct deferred_dtor_tracker deferred_dtor_tracker; /**< refcounted objects and arrays to call dtor on after unserializing. See i_zval_ptr_dtor */
#if PHP_VERSION_ID >= 70400
HashTable *ref_props; /**< objects&data for calls to __unserialize/__wakeup */
#endif
};
#define IGB_REF_VAL_2(igsd, n) ((igsd)->references[(n)])
#define IGB_NEEDS_MORE_DATA(igsd, n) UNEXPECTED((size_t)((igsd)->buffer_end - (igsd)->buffer_ptr) < (n))
#define IGB_REMAINING_BYTES(igsd) ((unsigned int)((igsd)->buffer_end - (igsd)->buffer_ptr))
#define IGB_BUFFER_OFFSET(igsd) ((unsigned int)((igsd)->buffer_ptr - (igsd)->buffer))
#define WANT_CLEAR (0)
#define WANT_REF (1 << 1)
/* }}} */
/* {{{ Serializing functions prototypes */
zend_always_inline static int igbinary_serialize_data_init(struct igbinary_serialize_data *igsd, bool scalar);
zend_always_inline static void igbinary_serialize_data_deinit(struct igbinary_serialize_data *igsd);
zend_always_inline static void igbinary_serialize_header(struct igbinary_serialize_data *igsd);
zend_always_inline static int igbinary_serialize8(struct igbinary_serialize_data *igsd, uint8_t i);
zend_always_inline static int igbinary_serialize16(struct igbinary_serialize_data *igsd, uint16_t i);
zend_always_inline static int igbinary_serialize32(struct igbinary_serialize_data *igsd, uint32_t i);
zend_always_inline static int igbinary_serialize64(struct igbinary_serialize_data *igsd, uint64_t i);
zend_always_inline static int igbinary_serialize_null(struct igbinary_serialize_data *igsd);
zend_always_inline static int igbinary_serialize_bool(struct igbinary_serialize_data *igsd, int b);
zend_always_inline static int igbinary_serialize_long(struct igbinary_serialize_data *igsd, zend_long l);
zend_always_inline static int igbinary_serialize_double(struct igbinary_serialize_data *igsd, double d);
zend_always_inline static int igbinary_serialize_string(struct igbinary_serialize_data *igsd, zend_string *s);
zend_always_inline static int igbinary_serialize_chararray(struct igbinary_serialize_data *igsd, const char *s, size_t len);
zend_always_inline static int igbinary_serialize_array(struct igbinary_serialize_data *igsd, zval *z, bool object, bool incomplete_class, bool serialize_props);
zend_always_inline static int igbinary_serialize_array_ref(struct igbinary_serialize_data *igsd, zval *z, bool object);
zend_always_inline static int igbinary_serialize_array_sleep(struct igbinary_serialize_data *igsd, zval *z, HashTable *ht, zend_class_entry *ce);
zend_always_inline static int igbinary_serialize_object_name(struct igbinary_serialize_data *igsd, zend_string *name);
zend_always_inline static int igbinary_serialize_object(struct igbinary_serialize_data *igsd, zval *z);
static int igbinary_serialize_zval(struct igbinary_serialize_data *igsd, zval *z);
/* }}} */
/* {{{ Unserializing functions prototypes */
zend_always_inline static int igbinary_unserialize_data_init(struct igbinary_unserialize_data *igsd);
zend_always_inline static void igbinary_unserialize_data_deinit(struct igbinary_unserialize_data *igsd);
zend_always_inline static int igbinary_unserialize_header(struct igbinary_unserialize_data *igsd);
zend_always_inline static uint8_t igbinary_unserialize8(struct igbinary_unserialize_data *igsd);
zend_always_inline static uint16_t igbinary_unserialize16(struct igbinary_unserialize_data *igsd);
zend_always_inline static uint32_t igbinary_unserialize32(struct igbinary_unserialize_data *igsd);
zend_always_inline static uint64_t igbinary_unserialize64(struct igbinary_unserialize_data *igsd);
zend_always_inline static int igbinary_unserialize_long(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zend_long *ret);
zend_always_inline static int igbinary_unserialize_double(struct igbinary_unserialize_data *igsd, double *ret);
zend_always_inline static zend_string *igbinary_unserialize_string(struct igbinary_unserialize_data *igsd, enum igbinary_type t);
zend_always_inline static zend_string *igbinary_unserialize_chararray(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zend_bool check_interned);
zend_always_inline static int igbinary_unserialize_array(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags, zend_bool create_ref);
zend_always_inline static int igbinary_unserialize_object(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags);
static int igbinary_unserialize_object_ser(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, zend_class_entry *ce);
static int igbinary_unserialize_zval(struct igbinary_unserialize_data *igsd, zval *const z, int flags);
/* }}} */
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_igbinary_serialize, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_igbinary_unserialize, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ igbinary_functions[] */
/** Exported php functions. */
zend_function_entry igbinary_functions[] = {
PHP_FE(igbinary_serialize, arginfo_igbinary_serialize)
PHP_FE(igbinary_unserialize, arginfo_igbinary_unserialize)
PHP_FE_END
};
/* }}} */
/* {{{ igbinary dependencies */
static const zend_module_dep igbinary_module_deps[] = {
ZEND_MOD_REQUIRED("standard")
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
ZEND_MOD_REQUIRED("session")
#endif
#if defined(HAVE_APCU_SUPPORT)
ZEND_MOD_OPTIONAL("apcu")
#endif
ZEND_MOD_END
};
/* }}} */
/* {{{ igbinary_module_entry */
zend_module_entry igbinary_module_entry = {
STANDARD_MODULE_HEADER_EX, NULL,
igbinary_module_deps,
"igbinary",
igbinary_functions,
PHP_MINIT(igbinary),
PHP_MSHUTDOWN(igbinary),
NULL,
NULL,
PHP_MINFO(igbinary),
PHP_IGBINARY_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(igbinary)
/* {{{ ZEND_GET_MODULE */
#ifdef COMPILE_DL_IGBINARY
ZEND_GET_MODULE(igbinary)
#endif
/* }}} */
/* {{{ INI entries */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("igbinary.compact_strings", "1", PHP_INI_ALL, OnUpdateBool, compact_strings, zend_igbinary_globals, igbinary_globals)
PHP_INI_END()
/* }}} */
/* {{{ php_igbinary_init_globals */
static void php_igbinary_init_globals(zend_igbinary_globals *igbinary_globals) {
igbinary_globals->compact_strings = 1;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
/**
* The module init function.
* This allocates the persistent resources of this PHP module.
*/
PHP_MINIT_FUNCTION(igbinary) {
(void)type;
(void)module_number;
ZEND_INIT_MODULE_GLOBALS(igbinary, php_igbinary_init_globals, NULL);
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
php_session_register_serializer("igbinary",
PS_SERIALIZER_ENCODE_NAME(igbinary),
PS_SERIALIZER_DECODE_NAME(igbinary));
#endif
#if defined(HAVE_APCU_SUPPORT)
apc_register_serializer("igbinary",
APC_SERIALIZER_NAME(igbinary),
APC_UNSERIALIZER_NAME(igbinary),
NULL);
#endif
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
/**
* The module shutdown function.
* This cleans up all persistent resources of this PHP module.
*/
PHP_MSHUTDOWN_FUNCTION(igbinary) {
(void)type;
(void)module_number;
#ifdef ZTS
ts_free_id(igbinary_globals_id);
#endif
/*
* Clean up ini entries.
* Aside: It seems like the php_session_register_serializer unserializes itself, since MSHUTDOWN in ext/wddx/wddx.c doesn't exist?
*/
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(igbinary) {
(void)zend_module;
php_info_print_table_start();
php_info_print_table_row(2, "igbinary support", "enabled");
php_info_print_table_row(2, "igbinary version", PHP_IGBINARY_VERSION);
#if defined(HAVE_APCU_SUPPORT)
php_info_print_table_row(2, "igbinary APCu serializer ABI", APC_SERIALIZER_ABI);
#else
php_info_print_table_row(2, "igbinary APCu serializer ABI", "no");
#endif
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
php_info_print_table_row(2, "igbinary session support", "yes");
#else
php_info_print_table_row(2, "igbinary session support", "no");
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ igsd management */
/* Append to list of references to take out later. Returns SIZE_MAX on allocation error. */
static zend_always_inline size_t igsd_append_ref(struct igbinary_unserialize_data *igsd, struct igbinary_value_ref v)
{
size_t ref_n;
if (igsd->references_count + 1 >= igsd->references_capacity) {
igsd->references_capacity *= 2;
struct igbinary_value_ref *new_references = erealloc(igsd->references, sizeof(igsd->references[0]) * igsd->references_capacity);
if (UNEXPECTED(new_references == NULL)) {
return SIZE_MAX;
}
igsd->references = new_references;
}
ref_n = igsd->references_count++;
IGB_REF_VAL_2(igsd, ref_n) = v;
return ref_n;
}
static zend_always_inline int igsd_ensure_defer_capacity(struct igbinary_unserialize_data *igsd) {
if (igsd->deferred_count >= igsd->deferred_capacity) {
if (igsd->deferred_capacity == 0) {
igsd->deferred_capacity = 2;
igsd->deferred = emalloc(sizeof(igsd->deferred[0]) * igsd->deferred_capacity);
} else {
igsd->deferred_capacity *= 2;
struct deferred_call *old_deferred = igsd->deferred;
igsd->deferred = erealloc(old_deferred, sizeof(igsd->deferred[0]) * igsd->deferred_capacity);
if (UNEXPECTED(igsd->deferred == NULL)) {
igsd->deferred = old_deferred;
return 1;
}
}
}
return 0;
}
static inline int igsd_defer_wakeup(struct igbinary_unserialize_data *igsd, zend_object *object) {
// TODO: This won't be properly garbage collected if there is an OOM error, but would php terminate instead?
RETURN_1_IF_NON_ZERO(igsd_ensure_defer_capacity(igsd));
struct deferred_call *c = &igsd->deferred[igsd->deferred_count++];
c->data.wakeup = object;
#if PHP_VERSION_ID >= 70400
c->is_unserialize = 0;
#endif
return 0;
}
/* igsd_defer_unserialize {{{ */
#if PHP_VERSION_ID >= 70400
static inline int igsd_defer_unserialize(struct igbinary_unserialize_data *igsd, zend_object *object, zval param) {
RETURN_1_IF_NON_ZERO(igsd_ensure_defer_capacity(igsd));
struct deferred_call *c = &igsd->deferred[igsd->deferred_count++];
struct deferred_unserialize_call* call = &c->data.unserialize;
call->object = object;
ZEND_ASSERT(Z_TYPE(param) == IS_ARRAY);
call->param = param;
c->is_unserialize = 1;
return 0;
}
#endif
/* }}} */
/* {{{ igbinary_finish_deferred_calls
* After all object instances were unserialized, perform the deferred calls to __wakeup() on all of the objects implementing that method.
*/
static int igbinary_finish_deferred_calls(struct igbinary_unserialize_data *igsd) {
#if PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 80000
zval unserialize_name;
#endif
zval wakeup_name;
uint32_t i;
struct deferred_call *deferred_arr;
uint32_t deferred_count = igsd->deferred_count;
zend_bool delayed_call_failed = 0;
igsd->deferred_finished = 1;
if (deferred_count == 0) { /* nothing to do */
return 0;
}
deferred_arr = igsd->deferred;
#if PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 80000
ZVAL_STRINGL(&unserialize_name, "__unserialize", sizeof("__unserialize") - 1);
#endif
ZVAL_STRINGL(&wakeup_name, "__wakeup", sizeof("__wakeup") - 1);
for (i = 0; i < deferred_count; i++) {
struct deferred_call *deferred = &deferred_arr[i];
#if PHP_VERSION_ID >= 70400
if (deferred->is_unserialize) {
struct deferred_unserialize_call *unserialize_call = &deferred->data.unserialize;
zend_object *const obj = unserialize_call->object;
ZEND_ASSERT(Z_TYPE(unserialize_call->param) == IS_ARRAY);
if (!delayed_call_failed) {
#if PHP_VERSION_ID >= 80000
/* Copy the parameter for __unserialize so that changes in __unserialize won't mutate the original. */
// ZVAL_COPY(¶m, &unserialize_call->param);
BG(serialize_lock)++;
zend_call_known_instance_method_with_1_params(
obj->ce->__unserialize, obj, NULL, &unserialize_call->param);
if (EG(exception)) {
delayed_call_failed = 1;
GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
}
BG(serialize_lock)--;
#else
zval retval;
zval zv;
ZVAL_OBJ(&zv, obj);
/* Copy the parameter for __unserialize so that changes in __unserialize won't mutate the original. */
// ZVAL_COPY(¶m, &unserialize_call->param);
BG(serialize_lock)++;
if (call_user_function(CG(function_table), &zv, &unserialize_name, &retval, 1, &unserialize_call->param) == FAILURE || Z_ISUNDEF(retval))
{
delayed_call_failed = 1;
GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
}
BG(serialize_lock)--;
zval_ptr_dtor(&retval);
#endif
} else {
GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
}
zval_ptr_dtor(&unserialize_call->param);
} else
#endif
{
zend_object *obj = deferred->data.wakeup;
if (!delayed_call_failed) {
zval retval; /* return value of __wakeup */
zval rval;
ZVAL_OBJ(&rval, obj);
if (UNEXPECTED(call_user_function(CG(function_table), &rval, &wakeup_name, &retval, 0, 0) == FAILURE || Z_ISUNDEF(retval))) {
delayed_call_failed = 1;
GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
}
zval_ptr_dtor(&retval);
} else {
GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
}
}
}
zval_ptr_dtor_str(&wakeup_name);
#if PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 80000
zval_ptr_dtor_str(&unserialize_name);
#endif
return delayed_call_failed;
}
/* }}} */
/* }}} */
/* {{{ igsd_ensure_deferred_dtor_capacity(struct igbinary_serialize_data *igsd) */
static inline int igsd_ensure_deferred_dtor_capacity(struct deferred_dtor_tracker *tracker) {
if (tracker->count >= tracker->capacity) {
if (tracker->capacity == 0) {
tracker->capacity = 2;
tracker->zvals = emalloc(sizeof(tracker->zvals[0]) * tracker->capacity);
} else {
tracker->capacity *= 2;
zval *old_deferred_dtor = tracker->zvals;
tracker->zvals = erealloc(old_deferred_dtor, sizeof(tracker->zvals[0]) * tracker->capacity);
if (UNEXPECTED(tracker->zvals == NULL)) {
tracker->zvals = old_deferred_dtor;
return 1;
}
}
}
return 0;
}
/* }}} */
/* {{{ free_deferred_dtors(struct deferred_dtor_tracker *tracker) */
static zend_always_inline void free_deferred_dtors(struct deferred_dtor_tracker *tracker) {
zval *const zvals = tracker->zvals;
if (zvals) {
const size_t n = tracker->count;
size_t i;
for (i = 0; i < n; i++) {
/* fprintf(stderr, "freeing i=%d id=%d refcount=%d\n", (int)i, (int)Z_OBJ_HANDLE(zvals[i]), (int)Z_REFCOUNT(zvals[i])); */
zval_ptr_dtor(&zvals[i]);
}
efree(zvals);
}
}
/* }}} */
/* {{{ igsd_addref_and_defer_dtor(struct igbinary_serialize_data *igsd, zval *z) */
static zend_always_inline int igsd_addref_and_defer_dtor(struct deferred_dtor_tracker *tracker, zval *z) {
if (!Z_REFCOUNTED_P(z)) {
return 0;
}
if (UNEXPECTED(igsd_ensure_deferred_dtor_capacity(tracker))) {
return 1;
}
ZEND_ASSERT(Z_REFCOUNT_P(z) >= 1); /* Expect that there were references at the time this was serialized */
ZVAL_COPY(&tracker->zvals[tracker->count++], z); /* Copy and increase reference count */
return 0;
}
/* }}} */
/* {{{ igsd_defer_dtor(struct igbinary_serialize_data *igsd, zval *z) */
static inline int igsd_defer_dtor(struct deferred_dtor_tracker *tracker, zval *z) {
if (!Z_REFCOUNTED_P(z)) {
return 0;
}
if (igsd_ensure_deferred_dtor_capacity(tracker)) {
return 1;
}
ZEND_ASSERT(Z_REFCOUNT_P(z) >= 1); /* Expect that there were references at the time this was serialized */
ZVAL_COPY_VALUE(&tracker->zvals[tracker->count++], z); /* Copy without increasing reference count */
return 0;
}
/* }}} */
/* {{{ int igbinary_serialize(uint8_t**, size_t*, zval*) */
IGBINARY_API int igbinary_serialize(uint8_t **ret, size_t *ret_len, zval *z) {
return igbinary_serialize_ex(ret, ret_len, z, NULL);
}
/* }}} */
/* {{{ int igbinary_serialize_ex(uint8_t**, size_t*, zval*, igbinary_memory_manager*) */
/**
* Serializes data, and writes the allocated byte buffer into ret and the buffer's length into ret_len.
* @param ret output parameter
* @param ret_len length of byte buffer ret
* @param z the zval (data) to serialize
* @param memory_manager (nullable) the memory manager to use for allocating/reallocating the buffer of serialized data. Used by extensions such as APCu
* @return 0 on success, 1 on failure
*/
IGBINARY_API int igbinary_serialize_ex(uint8_t **ret, size_t *ret_len, zval *z, struct igbinary_memory_manager *memory_manager) {
struct igbinary_serialize_data igsd;
uint8_t *tmpbuf;
int return_code;
// While we can't get passed references through the PHP_FUNCTIONs igbinary declares, third party code can invoke igbinary's methods with references.
// See https://github.com/php-memcached-dev/php-memcached/issues/326
if (UNEXPECTED(Z_TYPE_P(z) == IS_INDIRECT)) {
z = Z_INDIRECT_P(z);
}
ZVAL_DEREF(z);
if (UNEXPECTED(igbinary_serialize_data_init(&igsd, Z_TYPE_P(z) != IS_OBJECT && Z_TYPE_P(z) != IS_ARRAY))) {
zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
return 1;
}
igbinary_serialize_header(&igsd);
return_code = 0;
if (UNEXPECTED(igbinary_serialize_zval(&igsd, z) != 0)) {
return_code = 1;
goto cleanup;
}
/* Explicit null termination */
/* TODO: Stop doing this in the next major version, serialized data can contain nulls in the middle and callers should check length */
if (UNEXPECTED(igbinary_serialize8(&igsd, 0) != 0)) {
return_code = 1;
goto cleanup;
}
/* shrink buffer to the real length, ignore errors */
if (UNEXPECTED(memory_manager)) {
tmpbuf = memory_manager->alloc(igsd.buffer_size, memory_manager->context);
if (tmpbuf != NULL) {
memcpy(tmpbuf, igsd.buffer, igsd.buffer_size);
}
if (tmpbuf == NULL) {
return_code = 1;
goto cleanup;
}
*ret = tmpbuf;
*ret_len = igsd.buffer_size - 1;
} else {
/* Set return values */
*ret_len = igsd.buffer_size - 1;
*ret = igsd.buffer;
igsd.buffer = NULL;
}
cleanup:
igbinary_serialize_data_deinit(&igsd);
return return_code;
}
/* }}} */
/* {{{ int igbinary_unserialize(const uint8_t *, size_t, zval **) */
/**
* Unserializes the data into z
* @param buf the read-only buffer with the serialized data
* @param buf_len the length of that buffer.
* @param z output parameter. Will contain the unserialized value(zval).
* @return 0 on success, 1 on failure
*/
IGBINARY_API int igbinary_unserialize(const uint8_t *buf, size_t buf_len, zval *z) {
struct igbinary_unserialize_data igsd;
int ret = 0;
igbinary_unserialize_data_init(&igsd);
igsd.buffer = buf;
igsd.buffer_ptr = buf;
igsd.buffer_end = buf + buf_len;
if (UNEXPECTED(igbinary_unserialize_header(&igsd))) {
ret = 1;
goto cleanup;
}
if (UNEXPECTED(igbinary_unserialize_zval(&igsd, z, WANT_CLEAR))) {
ret = 1;
goto cleanup;
}
if (Z_REFCOUNTED_P(z)) {
#if PHP_VERSION_ID >= 70200
zend_refcounted *ref = Z_COUNTED_P(z);
gc_check_possible_root(ref);
#else
gc_check_possible_root(z);
#endif
}
if (UNEXPECTED(igsd.buffer_ptr < igsd.buffer_end)) {
// https://github.com/igbinary/igbinary/issues/64
zend_error(E_WARNING, "igbinary_unserialize: received more data to unserialize than expected");
ret = 1;
goto cleanup;
}
if (UNEXPECTED(igbinary_finish_deferred_calls(&igsd))) {
ret = 1;
goto cleanup;
}
cleanup:
igbinary_unserialize_data_deinit(&igsd);
return ret;
}
/* }}} */
/* {{{ proto string igbinary_unserialize(mixed value) */
/**
* @see igbinary.php for more detailed API documentation.
*/
PHP_FUNCTION(igbinary_unserialize) {
char *string = NULL;
size_t string_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &string, &string_len) == FAILURE) {
RETURN_NULL();
}
if (string_len <= 0) {
RETURN_FALSE;
}
if (igbinary_unserialize((uint8_t *)string, string_len, return_value) != 0) {
/* FIXME: is this a good place? a catch all */
zval_ptr_dtor(return_value);
RETURN_NULL();
}
}
/* }}} */
/* {{{ proto mixed igbinary_serialize(string value) */
/**
* @see igbinary.php for more detailed API documentation.
*/
PHP_FUNCTION(igbinary_serialize) {
zval *z;
uint8_t *string;
size_t string_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &z) == FAILURE) {
RETURN_NULL();
}
if (igbinary_serialize(&string, &string_len, z) != 0) {
RETURN_NULL();
}
RETVAL_STRINGL((char *)string, string_len);
efree(string);
}
/* }}} */
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
/* {{{ Serializer encode function */
/**
* This provides a serializer encode function for PHP's session module (using igbinary),
* if igbinary was compiled with session support.
*
* Session support has to be statically compiled into php to use igbinary,
* due to the lack of a cross-platform way to register a session serializer/unserializer
* when the session module isn't available.
*/
PS_SERIALIZER_ENCODE_FUNC(igbinary)
{
zval *session_vars;
zend_string *result;
struct igbinary_serialize_data igsd;
session_vars = &(PS(http_session_vars));
if (Z_ISREF_P(session_vars)) {
session_vars = Z_REFVAL_P(session_vars);
}
if (igbinary_serialize_data_init(&igsd, false)) {
zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
return ZSTR_EMPTY_ALLOC();
}
igbinary_serialize_header(&igsd);
/** We serialize the passed in array of session_var (including the empty array, for #231) */
/** the same way we would serialize a regular array. */
/** The corresponding PS_SERIALIZER_DECODE_FUNC will unserialize the array and individually add the session variables. */
if (igbinary_serialize_array(&igsd, session_vars, false, false, true) != 0) {
zend_error(E_WARNING, "igbinary_serialize: cannot serialize session variables");
result = ZSTR_EMPTY_ALLOC();
} else {
/* Copy the buffer to a new zend_string */
result = zend_string_init((const char *)igsd.buffer, igsd.buffer_size, 0);
}
igbinary_serialize_data_deinit(&igsd);
return result;
}
/* }}} */
/* {{{ Serializer decode function */
/**
* This provides a serializer decode function for PHP's session module (using igbinary),
* if igbinary was compiled with session support.
*
* Session support has to be statically compiled into php to use igbinary,
* due to the lack of a cross-platform way to register a session serializer/unserializer
* when the session module isn't available.
*
* This is similar to PS_SERIALIZER_DECODE_FUNC(php) from ext/session/session.c
*/
PS_SERIALIZER_DECODE_FUNC(igbinary) {
HashTable *tmp_hash;
zval z;
zval *d;
zend_string *key;
int ret = 0;
struct igbinary_unserialize_data igsd;
if (!val || vallen == 0) {
return SUCCESS;
}
if (igbinary_unserialize_data_init(&igsd) != 0) {
return FAILURE;
}
igsd.buffer = (const uint8_t *)val;
igsd.buffer_ptr = igsd.buffer;
igsd.buffer_end = igsd.buffer + vallen;
if (UNEXPECTED(igbinary_unserialize_header(&igsd))) {
ret = 1;
goto deinit;
}
/** The serializer serialized the session variables as an array. So, we unserialize that array. */
/** We then iterate over the array to set the individual session variables (managing the reference counts), then free the original array. */
if (UNEXPECTED(igbinary_unserialize_zval(&igsd, &z, WANT_CLEAR))) {
ret = 1;
goto deinit;
}
ret = igbinary_finish_deferred_calls(&igsd);
deinit:
igbinary_unserialize_data_deinit(&igsd);
if (UNEXPECTED(ret)) {
return FAILURE;
}
/* Validate that this is of the correct data type */
tmp_hash = HASH_OF(&z);
if (tmp_hash == NULL) {
zval_ptr_dtor(&z);
return FAILURE;
}
ZEND_HASH_FOREACH_STR_KEY_VAL(tmp_hash, key, d) {
if (key == NULL) { /* array key is a number, how? Skip it. */
/* ??? */
continue;
}
if (php_set_session_var(key, d, NULL)) { /* Added to session successfully */
/* Refcounted types such as arrays, objects, references need to have references incremented manually, so that zval_ptr_dtor doesn't clean up pointers they include. */
/* Non-refcounted types have the data copied. */
Z_TRY_ADDREF_P(d);
}
} ZEND_HASH_FOREACH_END();
zval_ptr_dtor(&z);
return SUCCESS;
}
/* }}} */
#endif /* HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION) */
#if defined(HAVE_APCU_SUPPORT)
/* {{{ apc_serialize function */
static int APC_SERIALIZER_NAME(igbinary) ( APC_SERIALIZER_ARGS ) {
(void)config;
if (igbinary_serialize(buf, buf_len, (zval *)value) == 0) {
/* flipped semantics - We return 1 to indicate success to APCu (and 0 for failure) */
return 1;
}
return 0;
}
/* }}} */
/* {{{ apc_unserialize function */
static int APC_UNSERIALIZER_NAME(igbinary) ( APC_UNSERIALIZER_ARGS ) {
(void)config;
if (igbinary_unserialize(buf, buf_len, value) == 0) {
/* flipped semantics - We return 1 to indicate success to APCu (and 0 for failure) */
return 1;
}
/* Failed. free return value */
zval_ptr_dtor(value);