-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigbinary_unserializer.cpp
856 lines (775 loc) · 31.3 KB
/
igbinary_unserializer.cpp
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
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author of hhvm fork: Tyson Andre <[email protected]> |
| Author of original igbinary: Oleg Grenrus <[email protected]>|
| See CREDITS for contributors |
+----------------------------------------------------------------------+
*/
/**
* Function for unserializing. This contains implementation details of igbinary_unserialize()
*/
#include "ext_igbinary.hpp"
// For HHVM_VERSION_*
#include "hphp/runtime/version.h"
#if HHVM_VERSION_MAJOR < 3 || (HHVM_VERSION_MAJOR == 3 && HHVM_VERSION_MINOR < 22)
#error Unsupported HHVM version
#endif
#include "hphp/runtime/base/array-init.h"
// for ::HPHP::collections::isType
#include "hphp/runtime/base/collections.h"
#include "hphp/runtime/base/execution-context.h"
// for req::vector
#include "hphp/runtime/base/req-containers.h"
#include "hphp/runtime/base/type-variant.h"
using namespace HPHP;
// TODO: Get rid of WANT_REF, similar to igbinary 2.0.5 for Zend
#define WANT_CLEAR (0)
#define WANT_REF (1<<1)
namespace {
const StaticString
s_unserialize("unserialize"),
s_PHP_Incomplete_Class("__PHP_Incomplete_Class"),
s_PHP_Incomplete_Class_Name("__PHP_Incomplete_Class_Name"),
s___wakeup("__wakeup");
/* {{{ data types */
/** Unserializer data.
* Uses RAII to ensure it is de-initialized.
* Based on data structure by Oleg Grenrus <[email protected]>
*/
struct igbinary_unserialize_data {
const uint8_t *buffer; /**< Buffer. */
const size_t buffer_size; /**< Buffer size. */
size_t buffer_offset; /**< Current read offset. */
// Containers using thread-local memory.
req::vector<String> strings; /**< Unserialized strings. */
req::vector<Variant*> references; /**< non-refcounted pointers to objects, arrays, and references being deserialized */
req::vector<Object> wakeup; /* objects for which to call __wakeup after unserialization is finished */
Array m_overwrittenList; /* Reference counted values that were overwritten. See base/variable-unserializer.cpp */
public:
igbinary_unserialize_data(const uint8_t* buf, size_t buf_size);
~igbinary_unserialize_data();
};
igbinary_unserialize_data::igbinary_unserialize_data(const uint8_t* buf, size_t buf_size) : buffer(buf), buffer_size(buf_size), buffer_offset(0), strings(0), references(0) {
}
igbinary_unserialize_data::~igbinary_unserialize_data() {
// Nothing to do for strings or references
// req::vector<Object> wakeup automatically handles reference counts?
}
/* }}} */
static void igbinary_unserialize_variant(igbinary_unserialize_data *igsd, Variant& v, int flags);
static bool igbinary_unserialize_array_key(igbinary_unserialize_data *igsd, Variant& v);
/* {{{ Unserializing functions prototypes */
/*
inline static void igbinary_unserialize_data_init(struct igbinary_unserialize_data *igsd);
inline static void igbinary_unserialize_data_deinit(struct igbinary_unserialize_data *igsd);
inline static void igbinary_unserialize_header(struct igbinary_unserialize_data *igsd);
inline static uint8_t igbinary_unserialize8(struct igbinary_unserialize_data *igsd);
inline static uint16_t igbinary_unserialize16(struct igbinary_unserialize_data *igsd);
inline static uint32_t igbinary_unserialize32(struct igbinary_unserialize_data *igsd);
inline static uint64_t igbinary_unserialize64(struct igbinary_unserialize_data *igsd);
inline static int64_t igbinary_unserialize_long(struct igbinary_unserialize_data *igsd, enum igbinary_type t);
inline static double igbinary_unserialize_double(struct igbinary_unserialize_data *igsd, enum igbinary_type t);
inline static String igbinary_unserialize_string(struct igbinary_unserialize_data *igsd, enum igbinary_type t);
inline static String igbinary_unserialize_chararray(struct igbinary_unserialize_data *igsd, enum igbinary_type t);
inline static void igbinary_unserialize_array(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Array& self, int flags);
inline static void igbinary_unserialize_object(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Object& self, int flags);
// inline static void igbinary_unserialize_object_ser(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Object& self, zend_class_entry *ce);
// inline static void igbinary_unserialize_ref(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags);
static void igbinary_unserialize_variant(struct igbinary_unserialize_data *igsd, Variant& v, int flags);
*/
/* }}} */
/* {{{ igsd_defer_wakeup */
/* Defer wakeup */
static inline void igsd_defer_wakeup(struct igbinary_unserialize_data *igsd, const Object& o) {
igsd->wakeup.emplace_back(o);
}
/* }}} */
/* {{{ igbinary_unserialize8 */
/** Unserialize 8bit value. */
inline static uint8_t igbinary_unserialize8(igbinary_unserialize_data *igsd) {
uint8_t ret = 0;
ret = igsd->buffer[igsd->buffer_offset++];
return ret;
}
/* }}} */
/* {{{ igbinary_unserialize16 */
/** Unserialize 16bit value. */
inline static uint16_t igbinary_unserialize16(igbinary_unserialize_data *igsd) {
uint16_t ret = 0;
ret |= ((uint16_t) igsd->buffer[igsd->buffer_offset++] << 8);
ret |= ((uint16_t) igsd->buffer[igsd->buffer_offset++] << 0);
return ret;
}
/* }}} */
/* {{{ igbinary_unserialize32 */
/** Unserialize 32bit value. */
inline static uint32_t igbinary_unserialize32(igbinary_unserialize_data *igsd) {
uint32_t ret = 0;
ret |= ((uint32_t) igsd->buffer[igsd->buffer_offset++] << 24);
ret |= ((uint32_t) igsd->buffer[igsd->buffer_offset++] << 16);
ret |= ((uint32_t) igsd->buffer[igsd->buffer_offset++] << 8);
ret |= ((uint32_t) igsd->buffer[igsd->buffer_offset++] << 0);
return ret;
}
/* }}} */
/* {{{ igbinary_unserialize64 */
/** Unserialize 64bit value. */
inline static uint64_t igbinary_unserialize64(igbinary_unserialize_data *igsd) {
uint64_t ret = 0;
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 56);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 48);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 40);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 32);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 24);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 16);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 8);
ret |= ((uint64_t) igsd->buffer[igsd->buffer_offset++] << 0);
return ret;
}
/* }}} */
/* {{{ igbinary_unserialize_header_throw_for_version */
/* Precondition: igsd->buffer_size >= 4 */
inline static void igbinary_unserialize_header_throw_for_version(struct igbinary_unserialize_data *igsd, int version) {
int i;
char buf[9], *it;
for (i = 0; i < 4; i++) {
if (!isprint((int)igsd->buffer[i])) {
if (version != 0 && (((unsigned int)version) & 0xff000000) == (unsigned int)version) {
// Check if high order byte was set instead of low order byte
throw IgbinaryWarning("igbinary_unserialize_header: unsupported version: %u, should be %u or %u (wrong endianness?)", (unsigned int) version, 0x00000001, (unsigned int) IGBINARY_FORMAT_VERSION);
}
// Binary data, or a version number from a future release.
throw IgbinaryWarning("igbinary_unserialize_header: unsupported version: %u, should be %u or %u", (int) version, 0x00000001, (int) IGBINARY_FORMAT_VERSION);
}
}
for (it = buf, i = 0; i < 4; i++) {
char c = igsd->buffer[i];
if (c == '"' || c == '\\') {
*it++ = '\\';
}
*it++ = c;
}
*it = '\0';
throw IgbinaryWarning("igbinary_unserialize_header: unsupported version: \"%s\"..., should begin with a binary version header of \"\\x00\\x00\\x00\\x01\" or \"\\x00\\x00\\x00\\x%02x\"", buf, (int)IGBINARY_FORMAT_VERSION);
}
/* }}} */
/* {{{ igbinary_unserialize_header */
/** Unserialize header. Check for version. */
inline static void igbinary_unserialize_header(struct igbinary_unserialize_data *igsd) {
uint32_t version;
if (igsd->buffer_offset + 5 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_header: expected at least 5 bytes of data, got %u byte(s)", (int)(igsd->buffer_size - igsd->buffer_offset));
}
version = igbinary_unserialize32(igsd);
/* Support older version 1 and the current format 2 */
if (version == IGBINARY_FORMAT_VERSION || version == 0x00000001) {
return;
} else {
igbinary_unserialize_header_throw_for_version(igsd, version);
}
}
/* }}} */
/* {{{ igbinary_unserialize_long */
/** Unserializes zend_long */
inline static int igbinary_unserialize_long(struct igbinary_unserialize_data *igsd, enum igbinary_type t) {
if (t == igbinary_type_long8p || t == igbinary_type_long8n) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_long: end-of-data");
}
return (t == igbinary_type_long8n ? -1 : 1) * igbinary_unserialize8(igsd);
} else if (t == igbinary_type_long16p || t == igbinary_type_long16n) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_long: end-of-data");
}
return (t == igbinary_type_long16n ? -1 : 1) * igbinary_unserialize16(igsd);
} else if (t == igbinary_type_long32p || t == igbinary_type_long32n) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_long: end-of-data");
}
/* check for boundaries */
return (t == igbinary_type_long32n ? -1 : 1) * igbinary_unserialize32(igsd);
} else if (t == igbinary_type_long64p || t == igbinary_type_long64n) {
if (igsd->buffer_offset + 8 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_long: end-of-data");
}
/* check for boundaries */
uint64_t tmp64 = igbinary_unserialize64(igsd);
if (tmp64 > 0x8000000000000000 || (tmp64 == 0x8000000000000000 && t == igbinary_type_long64p)) {
throw IgbinaryWarning("igbinary_unserialize_long: too big 64bit long.");
}
return (t == igbinary_type_long64n ? -1 : 1) * tmp64;
} else {
// FIXME is format string correct? Not reachable.
throw IgbinaryWarning("igbinary_unserialize_long: unknown type '%02x', position %ld", (unsigned char)t, igsd->buffer_offset);
}
return 0;
}
/* }}} */
/* {{{ igbinary_unserialize_double */
/** Unserializes double. */
inline static double igbinary_unserialize_double(struct igbinary_unserialize_data *igsd) {
union {
double d;
uint64_t u;
} u;
if (igsd->buffer_offset + 8 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_double: end-of-data");
}
u.u = igbinary_unserialize64(igsd);
return u.d;
}
/* }}} */
/* {{{ igbinary_unserialize_chararray */
/** Unserializes chararray of string. */
inline static const String& igbinary_unserialize_chararray(struct igbinary_unserialize_data *igsd, const enum igbinary_type t) {
size_t l;
if (t == igbinary_type_string8 || t == igbinary_type_object8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
l = igbinary_unserialize8(igsd);
} else if (t == igbinary_type_string16 || t == igbinary_type_object16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
l = igbinary_unserialize16(igsd);
if (igsd->buffer_offset + l > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
} else if (t == igbinary_type_string32 || t == igbinary_type_object32) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
l = igbinary_unserialize32(igsd);
if (igsd->buffer_offset + l > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
} else {
throw IgbinaryWarning("igbinary_unserialize_chararray: unknown type '0x%x', position %ld", (int)t, (int64_t)igsd->buffer_offset);
}
if (igsd->buffer_offset + l > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_chararray: end-of-data");
}
/** TODO : Optimize after implementing it the simple way and testing.. */
// Make a copy of every occurence of the string.
igsd->strings.emplace_back(reinterpret_cast<const char*>(igsd->buffer + igsd->buffer_offset), l, CopyString);
igsd->buffer_offset += l;
return igsd->strings.back();
}
/* }}} */
/* {{{ igbinary_unserialize_string */
/** Unserializes string. Unserializes by string id. */
inline static const String& igbinary_unserialize_string(struct igbinary_unserialize_data *igsd, enum igbinary_type t) {
size_t i;
if (t == igbinary_type_string_id8 || t == igbinary_type_object_id8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_string: end-of-data");
}
i = igbinary_unserialize8(igsd);
} else if (t == igbinary_type_string_id16 || t == igbinary_type_object_id16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_string: end-of-data");
}
i = igbinary_unserialize16(igsd);
} else if (t == igbinary_type_string_id32 || t == igbinary_type_object_id32) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_string: end-of-data");
}
i = igbinary_unserialize32(igsd);
} else {
throw IgbinaryWarning("igbinary_unserialize_string: unknown type '0x%x', position %ld", (int)t, (uint64_t)igsd->buffer_offset);
}
if (i >= igsd->strings.size()) {
throw IgbinaryWarning("igbinary_unserialize_string: string index is out-of-bounds");
}
return igsd->strings[i];
}
/* }}} */
/* {{{ igbinary_unserialize_object_prop */
/* Similar to unserializeProp. nProp is the number of remaining dynamic properties. */
inline static void igbinary_unserialize_object_prop(igbinary_unserialize_data *igsd, ObjectData* obj, const Variant& key, int nProp) {
// Do a two-step look up
// FIXME not sure how protected variables are handled in igbinary in php5. Try to imitate that.
// For now, assume it can be from the class or any parent class.
Variant* t;
if (UNLIKELY(key.isInteger())) {
// Integer keys are guaranteed to be dynamic properties.
t = &obj->reserveProperties(nProp).lvalAt(key, AccessFlags::Key);
} else {
const String strKey = key.toString();
auto const lookup = obj->getProp(nullptr, strKey.get());
if (!lookup.prop || !lookup.accessible) {
// Dynamic property. If this is the first, and we're using MixedArray,
// we need to pre-allocate space in the array to ensure the elements
// dont move during unserialization.
t = &obj->reserveProperties(nProp).lvalAt(strKey, AccessFlags::Key);
} else {
t = &tvAsVariant(lookup.prop);
}
}
if (UNLIKELY(isRefcountedType(t->getRawType()))) {
igsd->m_overwrittenList.append(*t);
// throw IgbinaryWarning("igbinary_unserialize_object_prop: TODO handle duplicate keys or overriding existing data");
//uns->putInOverwrittenList(*t);
}
igbinary_unserialize_variant(igsd, *t, WANT_CLEAR);
// FIXME Type check the unserialized data.
/*
if (!RuntimeOption::RepoAuthoritative) return;
if (!Repo::get().global().HardPrivatePropInference) return;
*/
/*
* We assume for performance reasons in repo authoriative mode that
* we can see all the sets to private properties in a class.
*
* It's a hole in this if we don't check unserialization doesn't
* violate what we've seen, which we handle by throwing if the repo
* was built with this option.
*/
/*
auto const cls = obj->getVMClass();
auto const slot = cls->lookupDeclProp(key.get());
if (UNLIKELY(slot == kInvalidSlot)) return;
auto const repoTy = obj->getVMClass()->declPropRepoAuthType(slot);
if (LIKELY(tvMatchesRepoAuthType(*t->asTypedValue(), repoTy))) {
return;
}
throwUnexpectedType(key, obj, *t);
*/
}
/* }}} */
/* {{{ igbinary_unserialize_object_new_contents_leftover */
/**
* Inefficiently unserialize the remaining properties of an object, given an incomplete object with class set but no properties.
* TODO: Pass this a list.
*/
inline static void igbinary_unserialize_object_new_contents_leftover(struct igbinary_unserialize_data* igsd, enum igbinary_type t, Object* obj, int n) {
//Class* objCls = obj->getVMClass();
for (int remainingProps = n; remainingProps > 0; --remainingProps) {
/*
use the number of properties remaining as an estimate for
the total number of dynamic properties when we see the
first dynamic prop. see getVariantPtr
*/
Variant v;
if (!igbinary_unserialize_array_key(igsd, v)) {
continue;
}
igbinary_unserialize_object_prop(igsd, obj->get(), v, remainingProps);
}
}
/* }}} */
/* {{{igbinary_unserialize_object_new_contents */
/**
* Unserialize the properties of an object, given an incomplete object with class set but no properties.
*/
inline static void igbinary_unserialize_object_new_contents(struct igbinary_unserialize_data* igsd, enum igbinary_type t, Object* obj) {
int n;
if (t == igbinary_type_array8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array: end-of-data");
}
n = igbinary_unserialize8(igsd);
} else if (t == igbinary_type_array16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array: end-of-data");
}
n = igbinary_unserialize16(igsd);
} else if (t == igbinary_type_array32) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object_contents: end-of-data");
}
n = igbinary_unserialize32(igsd);
} else {
throw IgbinaryWarning("igbinary_unserialize_object_contents: unknown type '%02x', position %lld", (int) t, (long long) igsd->buffer_offset);
}
/* n cannot be larger than the number of minimum "objects" in the array */
if (n > igsd->buffer_size - igsd->buffer_offset) {
throw IgbinaryWarning("igbinary_unserialize_object_contents: data size %lld smaller than requested array length %lld.", (long long)(igsd->buffer_size - igsd->buffer_offset), (long long)n);
}
if (obj->get()->isCollection()) {
throw IgbinaryWarning("igbinary_unserialize_object_contents: Cannot unserialize HPHP collections");
}
if (n == 0) {
return;
}
// FIXME: Iterate over object properties first(and figure out demangling), it's probably faster that way.
igbinary_unserialize_object_new_contents_leftover(igsd, t, obj, n);
// Wakeup will be deferred by caller.
}
/* }}} */
inline static void igbinary_unserialize_object_ser(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Object& obj) {
if (!obj->instanceof(SystemLib::s_SerializableClass)) {
raise_error("igbinary_unserialize: Class %s has no unserializer",
obj->getClassName().data());
return;
}
size_t n;
if (t == igbinary_type_object_ser8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object_ser: end-of-data");
}
n = igbinary_unserialize8(igsd TSRMLS_CC);
} else if (t == igbinary_type_object_ser16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object_ser: end-of-data");
}
n = igbinary_unserialize16(igsd TSRMLS_CC);
} else if (t == igbinary_type_object_ser32) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object_ser: end-of-data");
}
n = igbinary_unserialize32(igsd TSRMLS_CC);
} else {
throw IgbinaryWarning("igbinary_unserialize_object_ser: unknown type '%02x', position %llu", (int)t, (unsigned long long)igsd->buffer_offset);
}
if (igsd->buffer_offset + n > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object_ser: end-of-data");
}
String serialized = String::attach(makeStaticString(reinterpret_cast<const char*>(igsd->buffer + igsd->buffer_offset), n));
obj->o_invoke_few_args(s_unserialize, 1, serialized);
igsd->buffer_offset += n;
obj.get()->clearNoDestruct(); // Allow destructor to be called (???)
}
/** Unserialize object, store into v. */
inline static void igbinary_unserialize_object(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Variant& v, int flags) {
String class_name;
if (t == igbinary_type_object8 || t == igbinary_type_object16 || t == igbinary_type_object32) {
class_name = igbinary_unserialize_chararray(igsd, t);
} else if (t == igbinary_type_object_id8 || t == igbinary_type_object_id16 || t == igbinary_type_object_id32) {
class_name = igbinary_unserialize_string(igsd, t);
} else {
throw IgbinaryWarning("igbinary_unserialize_object: unknown object type '%02x', position %llu", (int)t, (long long)igsd->buffer_offset);
}
// Unserialize the inner type (The byte after the class name).
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_object: end-of-data");
}
t = (enum igbinary_type) igbinary_unserialize8(igsd);
Class* cls = Unit::loadClass(class_name.get()); // with autoloading
Object obj;
if (cls) {
// Only unserialize CPP extension types which can actually
// support it. Otherwise, we risk creating a CPP object
// without having it initialized completely.
if (cls->instanceCtor() && !cls->isCppSerializable() &&
!cls->isCollectionClass()) {
// TODO: Make corresponding check when serializing?
throw IgbinaryWarning("igbinary_unserialize_object: Unable to completely unserialize internal cpp class");
} else {
if (UNLIKELY(
collections::isType(cls, CollectionType::Pair)
)) { // && (size != 2))) {
throw IgbinaryWarning("igbinary_unserialize_object: HPHP type Pair unsupported, incompatible with php5/php7 implementation");
}
switch(t) {
case igbinary_type_array8:
case igbinary_type_array16:
case igbinary_type_array32:
obj = Object{cls};
break;
default:
obj = Object::attach(g_context->createObject(cls, init_null_variant, false));
break;
}
}
} else {
obj = Object{SystemLib::s___PHP_Incomplete_ClassClass};
obj->o_set(s_PHP_Incomplete_Class_Name, class_name);
}
v = obj;
if (flags & WANT_REF) {
v.asRef();
}
// FIXME custom unserializer?
/* add this to the list of unserialized references, get the index */
// FIXME support refs
// *obj will remain valid until __wakeup is called, which is done at the very end.
igsd->references.push_back(&v); // FIXME: Account for flags & WANT_REF
switch (t) {
case igbinary_type_array8:
case igbinary_type_array16:
case igbinary_type_array32:
igbinary_unserialize_object_new_contents(igsd, t, &obj);
break;
case igbinary_type_object_ser8:
case igbinary_type_object_ser16:
case igbinary_type_object_ser32:
igbinary_unserialize_object_ser(igsd, t, obj);
break;
default:
throw IgbinaryWarning("igbinary_unserialize_object: unknown object inner type '%02x', position %lld", (int)t, (long long)igsd->buffer_offset);
}
if (cls && cls->lookupMethod(s___wakeup.get())) {
igsd_defer_wakeup(igsd, obj);
}
}
/* }}} */
/* {{{ igbinary_unserialize_array_key */
/**
* Unserialize an array key (int64 or string).
* Postcondition: v.isInteger() || v.isString() || return value is false, or IgbinaryWarning thrown.
* See igbinary_unserialize_variant.
* (The return false if the serializer needs to indicate that the serialized entry was skipped)
*/
static bool igbinary_unserialize_array_key(igbinary_unserialize_data *igsd, Variant& v) {
enum igbinary_type t;
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array_key: end-of-data");
}
t = (enum igbinary_type) igbinary_unserialize8(igsd);
switch (t) {
case igbinary_type_string_empty:
{
String s = "";
// TODO: Make a constant?
tvMove(make_tv<KindOfString>(s.detach()), *v.asTypedValue());
}
return true;
case igbinary_type_long8p:
case igbinary_type_long8n:
case igbinary_type_long16p:
case igbinary_type_long16n:
case igbinary_type_long32p:
case igbinary_type_long32n:
case igbinary_type_long64p:
case igbinary_type_long64n:
v = igbinary_unserialize_long(igsd, t);
return true;
case igbinary_type_string8:
case igbinary_type_string16:
case igbinary_type_string32:
v = igbinary_unserialize_chararray(igsd, t);
return true;
case igbinary_type_string_id8:
case igbinary_type_string_id16:
case igbinary_type_string_id32:
v = igbinary_unserialize_string(igsd, t);
return true;
case igbinary_type_null:
return false;
default:
throw IgbinaryWarning("igbinary_unserialize_array_key: Unexpected igbinary_type 0x%02x at offset %lld", (int) t, (long long) igsd->buffer_offset);
}
return true;
}
/* {{{ igbinary_unserialize_array */
/** Unserializes array. */
inline static void igbinary_unserialize_array(struct igbinary_unserialize_data *igsd, enum igbinary_type t, Variant& v, bool wantRef) {
/* wantRef means that z will be wrapped by an IS_REFERENCE */
size_t n;
if (t == igbinary_type_array8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array: end-of-data");
}
n = igbinary_unserialize8(igsd);
} else if (t == igbinary_type_array16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array: end-of-data");
}
n = igbinary_unserialize16(igsd);
} else if (t == igbinary_type_array32) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_array: end-of-data");
}
n = igbinary_unserialize32(igsd);
} else {
throw IgbinaryWarning("igbinary_unserialize_array: unknown type 0x%02x, position %ld", t, igsd->buffer_offset);
}
/* n cannot be larger than the number of minimum "objects" in the array */
if (n > igsd->buffer_size - igsd->buffer_offset) {
throw IgbinaryWarning("igbinary_unserialize_array: data size %llu smaller that requested array length %llu.", (long long)(igsd->buffer_size - igsd->buffer_offset), (long long) n);
}
igsd->references.push_back(&v);
/* empty array */
if (n == 0) {
v = Array::Create(); // static empty array.
if (wantRef) {
v.asRef();
}
return;
}
v = ArrayInit(n, ArrayInit::Mixed{}).toArray();
// FIXME: Need to add a reference just in case of a duplicate key causing the original variant reference count to be decremented.
Array* arr;
if (wantRef) {
v.asRef();
auto tv = v.asTypedValue();
Variant& v_deref = *tv->m_data.pref->var();
arr = &(v_deref.asArrRef());
} else {
arr = &(v.asArrRef());
}
for (size_t i = 0; i < n; i++) {
Variant key;
if (!igbinary_unserialize_array_key(igsd, key)) {
continue;
}
// Postcondition: key.isString() || key.isInteger()
Variant& value = arr->lvalAt(key, AccessFlags::Key);
// FIXME: handle case of value already existing? (analogous to putInOverwrittenList)
// TODO: Any other code for references
igbinary_unserialize_variant(igsd, value, WANT_CLEAR);
}
}
/* }}} */
/* {{{ */
static void igbinary_unserialize_ref(igbinary_unserialize_data *igsd, enum igbinary_type t, Variant& v, int flags) {
size_t n;
if (t == igbinary_type_ref8 || t == igbinary_type_objref8) {
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_ref: end-of-data");
}
n = igbinary_unserialize8(igsd TSRMLS_CC);
} else if (t == igbinary_type_ref16 || t == igbinary_type_objref16) {
if (igsd->buffer_offset + 2 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_ref: end-of-data");
}
n = igbinary_unserialize16(igsd TSRMLS_CC);
} else if (LIKELY(t == igbinary_type_ref32 || t == igbinary_type_objref32)) {
if (igsd->buffer_offset + 4 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_ref: end-of-data");
}
n = igbinary_unserialize32(igsd TSRMLS_CC);
} else {
throw IgbinaryWarning("igbinary_unserialize_ref: unknown type '%02x', position %lld", (int)t, (long long)igsd->buffer_offset);
}
if (n >= igsd->references.size()) {
throw IgbinaryWarning("igbinary_unserialize_ref: invalid reference %u >= %u", (int) n, (int)igsd->references.size());
}
Variant*& data = igsd->references[n];
if ((flags & WANT_REF) != 0) {
if (data->getRawType() != KindOfRef) {
v = *data;
v.asRef();
data = &v;
} else {
v.assignRef(*data);
}
} else {
// Copy underlying data of ref or non-ref. Deletes old data?
// v.constructValHelper(*data);
cellDup(tvToInitCell(data->asTypedValue()), *v.asTypedValue());
}
}
/* }}} */
/* {{{ igbinary_unserialize_variant */
/* Unserialize a variant. Same as igbinary7 igbinary_unserialize_zval */
static void igbinary_unserialize_variant(igbinary_unserialize_data *igsd, Variant& v, int flags) {
enum igbinary_type t;
if (igsd->buffer_offset + 1 > igsd->buffer_size) {
throw IgbinaryWarning("igbinary_unserialize_variant: end-of-data");
}
t = (enum igbinary_type) igbinary_unserialize8(igsd);
switch (t) {
case igbinary_type_ref:
{
igbinary_unserialize_variant(igsd, v, WANT_REF);
const DataType type = v.getRawType();
/* If it is already a ref, nothing to do */
if (type == KindOfRef) {
break;
}
switch (type) {
case KindOfString:
case KindOfInt64:
case KindOfNull:
case KindOfDouble:
case KindOfBoolean:
igsd->references.push_back(&v);
break;
default:
break;
}
/* Convert v to a ref */
v.asRef();
}
break;
case igbinary_type_objref8:
case igbinary_type_objref16:
case igbinary_type_objref32:
case igbinary_type_ref8:
case igbinary_type_ref16:
case igbinary_type_ref32:
igbinary_unserialize_ref(igsd, t, v, flags);
return;
case igbinary_type_object8:
case igbinary_type_object16:
case igbinary_type_object32:
case igbinary_type_object_id8:
case igbinary_type_object_id16:
case igbinary_type_object_id32:
igbinary_unserialize_object(igsd, t, v, flags);
break;
case igbinary_type_array8:
case igbinary_type_array16:
case igbinary_type_array32:
{
igbinary_unserialize_array(igsd, t, v, (flags & WANT_REF) != 0);
}
break;
case igbinary_type_string_empty:
{
String s = "";
// TODO: Make a constant?
tvMove(make_tv<KindOfString>(s.detach()), *v.asTypedValue());
}
return;
case igbinary_type_long8p:
case igbinary_type_long8n:
case igbinary_type_long16p:
case igbinary_type_long16n:
case igbinary_type_long32p:
case igbinary_type_long32n:
case igbinary_type_long64p:
case igbinary_type_long64n:
v = igbinary_unserialize_long(igsd, t);
break;
case igbinary_type_string8:
case igbinary_type_string16:
case igbinary_type_string32:
v = igbinary_unserialize_chararray(igsd, t);
break;
case igbinary_type_string_id8:
case igbinary_type_string_id16:
case igbinary_type_string_id32:
v = igbinary_unserialize_string(igsd, t);
break;
case igbinary_type_double:
v = igbinary_unserialize_double(igsd);
break;
case igbinary_type_null:
v.setNull();
return;
case igbinary_type_bool_false:
v = false;
return;
case igbinary_type_bool_true:
v = true;
return;
default:
throw IgbinaryWarning("TODO implement igbinary_unserialize_variant for igbinary_type 0x%02x, offset %lld", (int) t, (long long) igsd->buffer_offset);
}
}
/* }}} */
} // namespace
namespace HPHP {
/** Unserialize the data, or clean up and throw an Exception. Effectively constant, unless __sleep modifies something. */
void igbinary_unserialize(const uint8_t *buf, size_t buf_len, Variant& v) {
igbinary_unserialize_data igsd(buf, buf_len); // initialized by constructor, freed by destructor
try {
igbinary_unserialize_header(&igsd); // Unserialize header or throw exception.
igbinary_unserialize_variant(&igsd, v, WANT_CLEAR);
/* FIXME finish_wakeup */
} catch (IgbinaryWarning &e) {
v.setNull();
raise_warning(e.getMessage());
return;
}
for (auto& obj : igsd.wakeup) {
obj->invokeWakeup();
}
}
} // namespace HPHP