-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathzend_object_handlers.c
More file actions
2682 lines (2378 loc) · 81.3 KB
/
zend_object_handlers.c
File metadata and controls
2682 lines (2378 loc) · 81.3 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
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@php.net> |
| Zeev Suraski <zeev@php.net> |
| Dmitry Stogov <dmitry@php.net> |
+----------------------------------------------------------------------+
*/
#include "zend.h"
#include "zend_globals.h"
#include "zend_lazy_objects.h"
#include "zend_variables.h"
#include "zend_API.h"
#include "zend_objects.h"
#include "zend_objects_API.h"
#include "zend_object_handlers.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "zend_closures.h"
#include "zend_compile.h"
#include "zend_hash.h"
#include "zend_property_hooks.h"
#include "zend_observer.h"
#define DEBUG_OBJECT_HANDLERS 0
#define ZEND_WRONG_PROPERTY_OFFSET 0
#define ZEND_HOOKED_PROPERTY_OFFSET 1
/* guard flags */
#define IN_GET ZEND_GUARD_PROPERTY_GET
#define IN_SET ZEND_GUARD_PROPERTY_SET
#define IN_UNSET ZEND_GUARD_PROPERTY_UNSET
#define IN_ISSET ZEND_GUARD_PROPERTY_ISSET
#define IN_HOOK ZEND_GUARD_PROPERTY_HOOK
static zend_arg_info zend_call_trampoline_arginfo[1] = {{0}};
static zend_arg_info zend_property_hook_arginfo[1] = {{0}};
static zend_always_inline bool zend_objects_check_stack_limit(void)
{
#ifdef ZEND_CHECK_STACK_LIMIT
return zend_call_stack_overflowed(EG(stack_limit));
#else
return false;
#endif
}
/*
__X accessors explanation:
if we have __get and property that is not part of the properties array is
requested, we call __get handler. If it fails, we return uninitialized.
if we have __set and property that is not part of the properties array is
set, we call __set handler. If it fails, we do not change the array.
for both handlers above, when we are inside __get/__set, no further calls for
__get/__set for this property of this object will be made, to prevent endless
recursion and enable accessors to change properties array.
if we have __call and method which is not part of the class function table is
called, we cal __call handler.
*/
ZEND_API HashTable *rebuild_object_properties_internal(zend_object *zobj) /* {{{ */
{
if (!zobj->properties) {
zend_property_info *prop_info;
zend_class_entry *ce = zobj->ce;
int i;
zobj->properties = zend_new_array(ce->default_properties_count);
if (ce->default_properties_count) {
zend_hash_real_init_mixed(zobj->properties);
for (i = 0; i < ce->default_properties_count; i++) {
prop_info = ce->properties_info_table[i];
if (!prop_info) {
continue;
}
if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
}
_zend_hash_append_ind(zobj->properties, prop_info->name,
OBJ_PROP(zobj, prop_info->offset));
}
}
}
return zobj->properties;
}
/* }}} */
/* Implements the fast path for array cast */
ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /* {{{ */
{
const zend_class_entry *ce = zobj->ce;
HashTable *ht;
zval* prop;
int i;
ZEND_ASSERT(!(zend_object_is_lazy_proxy(zobj) && zend_lazy_object_initialized(zobj)));
ZEND_ASSERT(!zobj->properties);
ht = zend_new_array(ce->default_properties_count);
if (ce->default_properties_count) {
zend_hash_real_init_mixed(ht);
for (i = 0; i < ce->default_properties_count; i++) {
const zend_property_info *prop_info = ce->properties_info_table[i];
if (!prop_info) {
continue;
}
prop = OBJ_PROP(zobj, prop_info->offset);
if (UNEXPECTED(Z_TYPE_P(prop) == IS_UNDEF)) {
continue;
}
if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
prop = Z_REFVAL_P(prop);
}
Z_TRY_ADDREF_P(prop);
_zend_hash_append(ht, prop_info->name, prop);
}
}
return ht;
}
/* }}} */
ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */
{
return zend_std_get_properties_ex(zobj);
}
/* }}} */
/* Fetch properties HashTable without triggering lazy initialization */
ZEND_API HashTable *zend_get_properties_no_lazy_init(zend_object *zobj)
{
if (zobj->handlers->get_properties == zend_std_get_properties) {
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
return zend_get_properties_no_lazy_init(instance);
}
if (!zobj->properties) {
rebuild_object_properties_internal(zobj);
}
return zobj->properties;
}
ZEND_ASSERT(!zend_object_is_lazy(zobj));
return zobj->handlers->get_properties(zobj);
}
ZEND_API HashTable *zend_std_get_gc(zend_object *zobj, zval **table, int *n) /* {{{ */
{
if (zobj->handlers->get_properties != zend_std_get_properties) {
*table = NULL;
*n = 0;
return zobj->handlers->get_properties(zobj);
} else {
if (UNEXPECTED(zend_object_is_lazy(zobj))) {
return zend_lazy_object_get_gc(zobj, table, n);
} else if (zobj->properties) {
*table = NULL;
*n = 0;
return zobj->properties;
} else {
*table = zobj->properties_table;
*n = zobj->ce->default_properties_count;
return NULL;
}
}
}
/* }}} */
ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
{
const zend_class_entry *ce = object->ce;
zval retval;
HashTable *ht;
if (!ce->__debugInfo) {
if (UNEXPECTED(zend_object_is_lazy(object))) {
return zend_lazy_object_debug_info(object, is_temp);
}
*is_temp = 0;
return object->handlers->get_properties(object);
}
zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval);
if (UNEXPECTED(Z_ISREF(retval))) {
zend_unwrap_reference(&retval);
}
if (Z_TYPE(retval) == IS_ARRAY) {
if (!Z_REFCOUNTED(retval)) {
*is_temp = 1;
return zend_array_dup(Z_ARRVAL(retval));
} else if (Z_REFCOUNT(retval) <= 1) {
*is_temp = 1;
ht = Z_ARR(retval);
return ht;
} else {
*is_temp = 0;
zval_ptr_dtor(&retval);
return Z_ARRVAL(retval);
}
} else if (Z_TYPE(retval) == IS_NULL) {
zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, return an empty array instead",
ZSTR_VAL(ce->name));
*is_temp = 1;
ht = zend_new_array(0);
return ht;
}
zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
}
/* }}} */
static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
{
zval member;
ZVAL_STR(&member, prop_name);
zend_call_known_instance_method_with_1_params(zobj->ce->__get, zobj, retval, &member);
}
/* }}} */
static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */
{
zval args[2];
ZVAL_STR(&args[0], prop_name);
ZVAL_COPY_VALUE(&args[1], value);
zend_call_known_instance_method(zobj->ce->__set, zobj, NULL, 2, args);
}
/* }}} */
static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */
{
zval member;
ZVAL_STR(&member, prop_name);
zend_call_known_instance_method_with_1_params(zobj->ce->__unset, zobj, NULL, &member);
}
/* }}} */
static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
{
zval member;
ZVAL_STR(&member, prop_name);
zend_call_known_instance_method_with_1_params(zobj->ce->__isset, zobj, retval, &member);
}
/* }}} */
static zend_always_inline bool is_derived_class(const zend_class_entry *child_class, const zend_class_entry *parent_class) /* {{{ */
{
child_class = child_class->parent;
while (child_class) {
if (child_class == parent_class) {
return 1;
}
child_class = child_class->parent;
}
return 0;
}
/* }}} */
static zend_never_inline int is_protected_compatible_scope(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */
{
return scope &&
(ce == scope || is_derived_class(ce, scope) || is_derived_class(scope, ce));
}
/* }}} */
static zend_never_inline zend_property_info *zend_get_parent_private_property(const zend_class_entry *scope, const zend_class_entry *ce, zend_string *member) /* {{{ */
{
zval *zv;
zend_property_info *prop_info;
if (scope != ce && scope && is_derived_class(ce, scope)) {
zv = zend_hash_find(&scope->properties_info, member);
if (zv != NULL) {
prop_info = (zend_property_info*)Z_PTR_P(zv);
if ((prop_info->flags & ZEND_ACC_PRIVATE)
&& prop_info->ce == scope) {
return prop_info;
}
}
}
return NULL;
}
/* }}} */
static ZEND_COLD zend_never_inline void zend_bad_property_access(const zend_property_info *property_info, const zend_class_entry *ce, const zend_string *member) /* {{{ */
{
zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
/* }}} */
static ZEND_COLD zend_never_inline void zend_bad_property_name(void) /* {{{ */
{
zend_throw_error(NULL, "Cannot access property starting with \"\\0\"");
}
/* }}} */
static ZEND_COLD zend_never_inline void zend_forbidden_dynamic_property(
const zend_class_entry *ce, const zend_string *member) {
zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
static ZEND_COLD zend_never_inline bool zend_deprecated_dynamic_property(
zend_object *obj, const zend_string *member) {
GC_ADDREF(obj);
zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
ZSTR_VAL(obj->ce->name), ZSTR_VAL(member));
if (UNEXPECTED(GC_DELREF(obj) == 0)) {
const zend_class_entry *ce = obj->ce;
zend_objects_store_del(obj);
if (!EG(exception)) {
/* We cannot continue execution and have to throw an exception */
zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
return 0;
}
return 1;
}
static ZEND_COLD zend_never_inline void zend_readonly_property_unset_error(
const zend_class_entry *ce, const zend_string *member) {
zend_throw_error(NULL, "Cannot unset readonly property %s::$%s",
ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
static zend_always_inline const zend_class_entry *get_fake_or_executed_scope(void)
{
if (UNEXPECTED(EG(fake_scope))) {
return EG(fake_scope);
} else {
return zend_get_executed_scope();
}
}
static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *ce, zend_string *member, int silent, void **cache_slot, const zend_property_info **info_ptr) /* {{{ */
{
zval *zv;
zend_property_info *property_info;
uint32_t flags;
uintptr_t offset;
if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) {
*info_ptr = CACHED_PTR_EX(cache_slot + 2);
return (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
}
if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
|| UNEXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
if (!silent) {
zend_bad_property_name();
}
return ZEND_WRONG_PROPERTY_OFFSET;
}
dynamic:
if (cache_slot) {
CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
CACHE_PTR_EX(cache_slot + 2, NULL);
}
return ZEND_DYNAMIC_PROPERTY_OFFSET;
}
property_info = (zend_property_info*)Z_PTR_P(zv);
flags = property_info->flags;
if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
const zend_class_entry *scope = get_fake_or_executed_scope();
if (property_info->ce != scope) {
if (flags & ZEND_ACC_CHANGED) {
zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
/* If there is a public/protected instance property on ce, don't try to use a
* private static property on scope. If both are static, prefer the static
* property on scope. This will throw a static property notice, rather than
* a visibility error. */
if (p && (!(p->flags & ZEND_ACC_STATIC) || (flags & ZEND_ACC_STATIC))) {
property_info = p;
flags = property_info->flags;
goto found;
} else if (flags & ZEND_ACC_PUBLIC) {
goto found;
}
}
if (flags & ZEND_ACC_PRIVATE) {
if (property_info->ce != ce) {
goto dynamic;
} else {
wrong:
/* Information was available, but we were denied access. Error out. */
if (!silent) {
zend_bad_property_access(property_info, ce, member);
}
return ZEND_WRONG_PROPERTY_OFFSET;
}
} else {
ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
goto wrong;
}
}
}
}
found:
if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
if (!silent) {
zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
return ZEND_DYNAMIC_PROPERTY_OFFSET;
}
if (property_info->hooks) {
*info_ptr = property_info;
if (cache_slot) {
CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_HOOKED_PROPERTY_OFFSET);
CACHE_PTR_EX(cache_slot + 2, property_info);
}
return ZEND_HOOKED_PROPERTY_OFFSET;
}
offset = property_info->offset;
if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
property_info = NULL;
} else {
*info_ptr = property_info;
}
if (cache_slot) {
CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset);
CACHE_PTR_EX(cache_slot + 2, property_info);
}
return offset;
}
/* }}} */
static ZEND_COLD void zend_wrong_offset(zend_class_entry *ce, zend_string *member) /* {{{ */
{
const zend_property_info *dummy;
/* Trigger the correct error */
zend_get_property_offset(ce, member, 0, NULL, &dummy);
}
/* }}} */
ZEND_API zend_property_info *zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent) /* {{{ */
{
zval *zv;
zend_property_info *property_info;
uint32_t flags;
if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
|| EXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
if (!silent) {
zend_bad_property_name();
}
return ZEND_WRONG_PROPERTY_INFO;
}
dynamic:
return NULL;
}
property_info = (zend_property_info*)Z_PTR_P(zv);
flags = property_info->flags;
if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
const zend_class_entry *scope = get_fake_or_executed_scope();
if (property_info->ce != scope) {
if (flags & ZEND_ACC_CHANGED) {
zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
if (p) {
property_info = p;
flags = property_info->flags;
goto found;
} else if (flags & ZEND_ACC_PUBLIC) {
goto found;
}
}
if (flags & ZEND_ACC_PRIVATE) {
if (property_info->ce != ce) {
goto dynamic;
} else {
wrong:
/* Information was available, but we were denied access. Error out. */
if (!silent) {
zend_bad_property_access(property_info, ce, member);
}
return ZEND_WRONG_PROPERTY_INFO;
}
} else {
ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
goto wrong;
}
}
}
}
found:
if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
if (!silent) {
zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
}
}
return property_info;
}
/* }}} */
ZEND_API zend_result zend_check_property_access(const zend_object *zobj, zend_string *prop_info_name, bool is_dynamic) /* {{{ */
{
zend_property_info *property_info;
const char *class_name = NULL;
const char *prop_name;
zend_string *member;
size_t prop_name_len;
if (ZSTR_VAL(prop_info_name)[0] == 0) {
if (is_dynamic) {
return SUCCESS;
}
zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len);
member = zend_string_init(prop_name, prop_name_len, 0);
property_info = zend_get_property_info(zobj->ce, member, 1);
zend_string_release_ex(member, 0);
if (property_info == NULL || property_info == ZEND_WRONG_PROPERTY_INFO) {
return FAILURE;
}
if (class_name[0] != '*') {
if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
/* we we're looking for a private prop but found a non private one of the same name */
return FAILURE;
} else if (!zend_string_equals(prop_info_name, property_info->name)) {
/* we we're looking for a private prop but found a private one of the same name but another class */
return FAILURE;
}
} else {
/* We were looking for a protected property but found a private one
* belonging to the parent class. */
if (property_info->flags & ZEND_ACC_PRIVATE) {
return FAILURE;
}
ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED);
}
return SUCCESS;
} else {
property_info = zend_get_property_info(zobj->ce, prop_info_name, 1);
if (property_info == NULL) {
ZEND_ASSERT(is_dynamic);
return SUCCESS;
} else if (property_info == ZEND_WRONG_PROPERTY_INFO) {
return FAILURE;
}
return (property_info->flags & ZEND_ACC_PUBLIC) ? SUCCESS : FAILURE;
}
}
/* }}} */
ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_property_info *prop_info) {
ZEND_ASSERT(prop_info->flags & ZEND_ACC_PPP_SET_MASK);
ZEND_ASSERT(!(prop_info->flags & ZEND_ACC_PUBLIC_SET));
const zend_class_entry *scope = get_fake_or_executed_scope();
if (prop_info->ce == scope) {
return true;
}
return EXPECTED((prop_info->flags & ZEND_ACC_PROTECTED_SET)
&& is_protected_compatible_scope(prop_info->prototype->ce, scope));
}
static void zend_property_guard_dtor(zval *el) /* {{{ */ {
uint32_t *ptr = (uint32_t*)Z_PTR_P(el);
if (EXPECTED(!(((uintptr_t)ptr) & 1))) {
efree_size(ptr, sizeof(uint32_t));
}
}
/* }}} */
static zend_always_inline zval *zend_get_guard_value(zend_object *zobj)
{
return zobj->properties_table + zobj->ce->default_properties_count;
}
ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */
{
HashTable *guards;
zval *zv;
uint32_t *ptr;
ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS);
zv = zend_get_guard_value(zobj);
if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
zend_string *str = Z_STR_P(zv);
if (EXPECTED(str == member) ||
/* str and member don't necessarily have a pre-calculated hash value here */
EXPECTED(zend_string_equal_content(str, member))) {
return &Z_GUARD_P(zv);
} else if (EXPECTED(Z_GUARD_P(zv) == 0)) {
zval_ptr_dtor_str(zv);
ZVAL_STR_COPY(zv, member);
return &Z_GUARD_P(zv);
} else {
ALLOC_HASHTABLE(guards);
zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0);
/* mark pointer as "special" using low bit */
zend_hash_add_new_ptr(guards, str,
(void*)(((uintptr_t)&Z_GUARD_P(zv)) | 1));
zval_ptr_dtor_str(zv);
ZVAL_ARR(zv, guards);
}
} else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
guards = Z_ARRVAL_P(zv);
ZEND_ASSERT(guards != NULL);
zv = zend_hash_find(guards, member);
if (zv != NULL) {
return (uint32_t*)(((uintptr_t)Z_PTR_P(zv)) & ~1);
}
} else {
ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF);
ZVAL_STR_COPY(zv, member);
Z_GUARD_P(zv) &= ~ZEND_GUARD_PROPERTY_MASK;
return &Z_GUARD_P(zv);
}
/* we have to allocate uint32_t separately because ht->arData may be reallocated */
ptr = (uint32_t*)emalloc(sizeof(uint32_t));
*ptr = 0;
return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr);
}
/* }}} */
ZEND_API uint32_t *zend_get_recursion_guard(zend_object *zobj)
{
if (!(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
return NULL;
}
zval *zv = zend_get_guard_value(zobj);
return &Z_GUARD_P(zv);
}
ZEND_COLD static void zend_typed_property_uninitialized_access(const zend_property_info *prop_info, zend_string *name)
{
zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization",
ZSTR_VAL(prop_info->ce->name),
ZSTR_VAL(name));
}
static ZEND_FUNCTION(zend_parent_hook_get_trampoline);
static ZEND_FUNCTION(zend_parent_hook_set_trampoline);
static bool zend_is_in_hook(const zend_property_info *prop_info)
{
const zend_execute_data *execute_data = EG(current_execute_data);
if (!execute_data || !EX(func) || !EX(func)->common.prop_info) {
return false;
}
const zend_property_info *parent_info = EX(func)->common.prop_info;
ZEND_ASSERT(prop_info->prototype && parent_info->prototype);
return prop_info->prototype == parent_info->prototype;
}
static bool zend_should_call_hook(const zend_property_info *prop_info, const zend_object *obj)
{
if (!zend_is_in_hook(prop_info)) {
return true;
}
/* execute_data and This are guaranteed to be set if zend_is_in_hook() returns true. */
zend_object *parent_obj = Z_OBJ(EG(current_execute_data)->This);
if (parent_obj == obj) {
return false;
}
if (zend_object_is_lazy_proxy(parent_obj)
&& zend_lazy_object_initialized(parent_obj)
&& zend_lazy_object_get_instance(parent_obj) == obj) {
return false;
}
return true;
}
static ZEND_COLD void zend_throw_no_prop_backing_value_access(const zend_string *class_name, const zend_string *prop_name, bool is_read)
{
zend_throw_error(NULL, "Must not %s virtual property %s::$%s",
is_read ? "read from" : "write to",
ZSTR_VAL(class_name), ZSTR_VAL(prop_name));
}
static bool zend_call_get_hook(
const zend_property_info *prop_info, const zend_string *prop_name,
zend_function *get, zend_object *zobj, zval *rv)
{
if (!zend_should_call_hook(prop_info, zobj)) {
if (UNEXPECTED(prop_info->flags & ZEND_ACC_VIRTUAL)) {
zend_throw_no_prop_backing_value_access(zobj->ce->name, prop_name, /* is_read */ true);
}
return false;
}
GC_ADDREF(zobj);
zend_call_known_instance_method_with_0_params(get, zobj, rv);
OBJ_RELEASE(zobj);
return true;
}
ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
{
zval *retval;
uintptr_t property_offset;
const zend_property_info *prop_info = NULL;
uint32_t *guard = NULL;
#if DEBUG_OBJECT_HANDLERS
fprintf(stderr, "Read object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
#endif
/* make zend_get_property_info silent if we have getter - we may want to use it */
property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot, &prop_info);
if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
try_again:
retval = OBJ_PROP(zobj, property_offset);
if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))
&& (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
&& ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info))) {
if (Z_TYPE_P(retval) == IS_OBJECT) {
/* For objects, W/RW/UNSET fetch modes might not actually modify object.
* Similar as with magic __get() allow them, but return the value as a copy
* to make sure no actual modification is possible. */
ZVAL_COPY(rv, retval);
retval = rv;
goto exit;
} else if (Z_TYPE_P(retval) == IS_UNDEF && type == BP_VAR_UNSET) {
retval = &EG(uninitialized_zval);
goto exit;
}
if (prop_info->flags & ZEND_ACC_READONLY) {
zend_readonly_property_indirect_modification_error(prop_info);
} else {
zend_asymmetric_visibility_property_modification_error(prop_info, "indirectly modify");
}
retval = &EG(uninitialized_zval);
goto exit;
}
if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) {
goto exit;
}
if (UNEXPECTED(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT)) {
/* Skip __get() for uninitialized typed properties */
goto uninit_error;
}
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
if (EXPECTED(zobj->properties != NULL)) {
if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
if (EXPECTED(p->key == name) ||
(EXPECTED(p->h == ZSTR_H(name)) &&
EXPECTED(p->key != NULL) &&
EXPECTED(zend_string_equal_content(p->key, name)))) {
retval = &p->val;
goto exit;
}
}
CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
}
retval = zend_hash_find(zobj->properties, name);
if (EXPECTED(retval)) {
if (cache_slot) {
uintptr_t idx = (char*)retval - (char*)zobj->properties->arData;
CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
}
goto exit;
}
}
} else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET];
if (!get) {
if (prop_info->flags & ZEND_ACC_VIRTUAL) {
zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s",
ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
return &EG(uninitialized_zval);
}
/* Cache the fact that this hook has trivial read. This only applies to
* BP_VAR_R and BP_VAR_IS fetches. */
ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
retval = OBJ_PROP(zobj, prop_info->offset);
if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
/* As hooked properties can't be unset, the only way to end up with an undef
* value is via an uninitialized property. */
ZEND_ASSERT(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT);
goto uninit_error;
}
if (UNEXPECTED(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
if (UNEXPECTED(Z_TYPE_P(retval) != IS_OBJECT)) {
zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed",
ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
goto exit;
}
ZVAL_COPY(rv, retval);
retval = rv;
}
goto exit;
}
const zend_class_entry *ce = zobj->ce;
if (!zend_call_get_hook(prop_info, name, get, zobj, rv)) {
if (EG(exception)) {
return &EG(uninitialized_zval);
}
/* Reads from backing store can only occur in hooks, and hence will always remain simple. */
const zend_execute_data *execute_data = EG(current_execute_data);
if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_FETCH_OBJ_R && EX(opline)->op1_type == IS_UNUSED) {
ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
}
property_offset = prop_info->offset;
if (!ZEND_TYPE_IS_SET(prop_info->type)) {
prop_info = NULL;
}
goto try_again;
}
if (EXPECTED(cache_slot
&& zend_execute_ex == execute_ex
&& ce->default_object_handlers->read_property == zend_std_read_property
&& !ce->create_object
&& !zend_is_in_hook(prop_info)
&& !(prop_info->hooks[ZEND_PROPERTY_HOOK_GET]->common.fn_flags & ZEND_ACC_RETURN_REFERENCE))) {
ZEND_SET_PROPERTY_HOOK_SIMPLE_GET(cache_slot);
}
if (Z_TYPE_P(rv) != IS_UNDEF) {
retval = rv;
if (!Z_ISREF_P(rv)
&& (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
&& UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed",
ZSTR_VAL(ce->name), ZSTR_VAL(name));
}
} else {
retval = &EG(uninitialized_zval);
}
goto exit;
} else if (UNEXPECTED(EG(exception))) {
retval = &EG(uninitialized_zval);
goto exit;
}
retval = &EG(uninitialized_zval);
/* For initialized lazy proxies: if the real instance's magic method
* guard is already set for this property, we are inside a recursive
* call from the real instance's __get/__isset. Forward directly to
* the real instance to avoid double invocation. (GH-21478) */
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
uint32_t *instance_guard = zend_get_property_guard(instance, name);
uint32_t guard_type = ((type == BP_VAR_IS) && zobj->ce->__isset)
? IN_ISSET : IN_GET;
if ((*instance_guard) & guard_type) {
retval = zend_std_read_property(instance, name, type, cache_slot, rv);
if (retval == &EG(uninitialized_zval)) {
ZVAL_NULL(rv);
retval = rv;
}
return retval;
}
}
}
/* magic isset */
if ((type == BP_VAR_IS) && zobj->ce->__isset) {
zval tmp_result;
guard = zend_get_property_guard(zobj, name);
if (!((*guard) & IN_ISSET)) {
GC_ADDREF(zobj);
*guard |= IN_ISSET;
zend_std_call_issetter(zobj, name, &tmp_result);
*guard &= ~IN_ISSET;
if (!zend_is_true(&tmp_result)) {
retval = &EG(uninitialized_zval);
OBJ_RELEASE(zobj);
zval_ptr_dtor(&tmp_result);
goto exit;
}
zval_ptr_dtor(&tmp_result);
if (zobj->ce->__get && !((*guard) & IN_GET)) {
goto call_getter;
}
OBJ_RELEASE(zobj);
} else if (zobj->ce->__get && !((*guard) & IN_GET)) {
goto call_getter_addref;
}
} else if (zobj->ce->__get) {
/* magic get */
guard = zend_get_property_guard(zobj, name);
if (!((*guard) & IN_GET)) {
/* have getter - try with it! */
call_getter_addref:
GC_ADDREF(zobj);
call_getter:
*guard |= IN_GET; /* prevent circular getting */
zend_std_call_getter(zobj, name, rv);
*guard &= ~IN_GET;
if (Z_TYPE_P(rv) != IS_UNDEF) {
retval = rv;
if (!Z_ISREF_P(rv) &&
(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
}
}
} else {
retval = &EG(uninitialized_zval);
}
if (prop_info) {
zend_verify_prop_assignable_by_ref_ex(prop_info, retval, (zobj->ce->__get->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0, ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_MAGIC_GET);
}
OBJ_RELEASE(zobj);
goto exit;
} else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) {
/* Trigger the correct error */
zend_wrong_offset(zobj->ce, name);
ZEND_ASSERT(EG(exception));
retval = &EG(uninitialized_zval);
goto exit;
}
}
uninit_error:
if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
if (!prop_info || (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY)) {
zend_object *instance = zend_lazy_object_init(zobj);
if (!instance) {
retval = &EG(uninitialized_zval);
goto exit;
}
if (UNEXPECTED(guard && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS))) {
/* Find which guard was used on zobj, so we can set the same
* guard on instance. */
uint32_t guard_type = (type == BP_VAR_IS) && zobj->ce->__isset
? IN_ISSET : IN_GET;
guard = zend_get_property_guard(instance, name);
if (!((*guard) & guard_type)) {
(*guard) |= guard_type;