-
Notifications
You must be signed in to change notification settings - Fork 36
/
zend_inheritance.c
3105 lines (2758 loc) · 106 KB
/
zend_inheritance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <[email protected]> |
| Zeev Suraski <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "zend.h"
#include "zend_API.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_inheritance.h"
#include "zend_interfaces.h"
#include "zend_smart_str.h"
#include "zend_operators.h"
#include "zend_exceptions.h"
#include "zend_enum.h"
#include "zend_attributes.h"
#include "zend_constants.h"
ZEND_API zend_class_entry* (*zend_inheritance_cache_get)(zend_class_entry *ce, zend_class_entry *parent, zend_class_entry **traits_and_interfaces) = NULL;
ZEND_API zend_class_entry* (*zend_inheritance_cache_add)(zend_class_entry *ce, zend_class_entry *proto, zend_class_entry *parent, zend_class_entry **traits_and_interfaces, HashTable *dependencies) = NULL;
/* Unresolved means that class declarations that are currently not available are needed to
* determine whether the inheritance is valid or not. At runtime UNRESOLVED should be treated
* as an ERROR. */
typedef enum {
INHERITANCE_UNRESOLVED = -1,
INHERITANCE_ERROR = 0,
INHERITANCE_WARNING = 1,
INHERITANCE_SUCCESS = 2,
} inheritance_status;
static void add_dependency_obligation(zend_class_entry *ce, zend_class_entry *dependency_ce);
static void add_compatibility_obligation(
zend_class_entry *ce, const zend_function *child_fn, zend_class_entry *child_scope,
const zend_function *parent_fn, zend_class_entry *parent_scope);
static void add_property_compatibility_obligation(
zend_class_entry *ce, const zend_property_info *child_prop,
const zend_property_info *parent_prop);
static void ZEND_COLD emit_incompatible_method_error(
const zend_function *child, zend_class_entry *child_scope,
const zend_function *parent, zend_class_entry *parent_scope,
inheritance_status status);
static void zend_type_copy_ctor(zend_type *type, bool persistent) {
if (ZEND_TYPE_HAS_LIST(*type)) {
zend_type_list *old_list = ZEND_TYPE_LIST(*type);
size_t size = ZEND_TYPE_LIST_SIZE(old_list->num_types);
zend_type_list *new_list = ZEND_TYPE_USES_ARENA(*type)
? zend_arena_alloc(&CG(arena), size) : pemalloc(size, persistent);
memcpy(new_list, old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types));
ZEND_TYPE_SET_PTR(*type, new_list);
zend_type *list_type;
ZEND_TYPE_LIST_FOREACH(new_list, list_type) {
ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
zend_string_addref(ZEND_TYPE_NAME(*list_type));
} ZEND_TYPE_LIST_FOREACH_END();
} else if (ZEND_TYPE_HAS_NAME(*type)) {
zend_string_addref(ZEND_TYPE_NAME(*type));
}
}
static zend_function *zend_duplicate_internal_function(zend_function *func, zend_class_entry *ce) /* {{{ */
{
zend_function *new_function;
if (UNEXPECTED(ce->type & ZEND_INTERNAL_CLASS)) {
new_function = pemalloc(sizeof(zend_internal_function), 1);
memcpy(new_function, func, sizeof(zend_internal_function));
} else {
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_function, func, sizeof(zend_internal_function));
new_function->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
}
if (EXPECTED(new_function->common.function_name)) {
zend_string_addref(new_function->common.function_name);
}
return new_function;
}
/* }}} */
static zend_always_inline zend_function *zend_duplicate_function(zend_function *func, zend_class_entry *ce) /* {{{ */
{
if (UNEXPECTED(func->type == ZEND_INTERNAL_FUNCTION)) {
return zend_duplicate_internal_function(func, ce);
} else {
if (func->op_array.refcount) {
(*func->op_array.refcount)++;
}
if (EXPECTED(func->op_array.function_name)) {
zend_string_addref(func->op_array.function_name);
}
return func;
}
}
/* }}} */
static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
{
zend_class_entry *parent = ce->parent;
ZEND_ASSERT(parent != NULL);
/* You cannot change create_object */
ce->create_object = parent->create_object;
/* Inherit special functions if needed */
if (EXPECTED(!ce->get_iterator)) {
ce->get_iterator = parent->get_iterator;
}
if (EXPECTED(!ce->__get)) {
ce->__get = parent->__get;
}
if (EXPECTED(!ce->__set)) {
ce->__set = parent->__set;
}
if (EXPECTED(!ce->__unset)) {
ce->__unset = parent->__unset;
}
if (EXPECTED(!ce->__isset)) {
ce->__isset = parent->__isset;
}
if (EXPECTED(!ce->__call)) {
ce->__call = parent->__call;
}
if (EXPECTED(!ce->__callstatic)) {
ce->__callstatic = parent->__callstatic;
}
if (EXPECTED(!ce->__tostring)) {
ce->__tostring = parent->__tostring;
}
if (EXPECTED(!ce->clone)) {
ce->clone = parent->clone;
}
if (EXPECTED(!ce->__serialize)) {
ce->__serialize = parent->__serialize;
}
if (EXPECTED(!ce->__unserialize)) {
ce->__unserialize = parent->__unserialize;
}
if (EXPECTED(!ce->serialize)) {
ce->serialize = parent->serialize;
}
if (EXPECTED(!ce->unserialize)) {
ce->unserialize = parent->unserialize;
}
if (!ce->destructor) {
ce->destructor = parent->destructor;
}
if (EXPECTED(!ce->__debugInfo)) {
ce->__debugInfo = parent->__debugInfo;
}
if (ce->constructor) {
if (parent->constructor && UNEXPECTED(parent->constructor->common.fn_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_ERROR, "Cannot override final %s::%s() with %s::%s()",
ZSTR_VAL(parent->name), ZSTR_VAL(parent->constructor->common.function_name),
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
return;
}
ce->constructor = parent->constructor;
}
/* }}} */
char *zend_visibility_string(uint32_t fn_flags) /* {{{ */
{
if (fn_flags & ZEND_ACC_PUBLIC) {
return "public";
} else if (fn_flags & ZEND_ACC_PRIVATE) {
return "private";
} else {
ZEND_ASSERT(fn_flags & ZEND_ACC_PROTECTED);
return "protected";
}
}
/* }}} */
static zend_string *resolve_class_name(zend_class_entry *scope, zend_string *name) {
ZEND_ASSERT(scope);
if (zend_string_equals_literal_ci(name, "parent") && scope->parent) {
if (scope->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
return scope->parent->name;
} else {
return scope->parent_name;
}
} else if (zend_string_equals_literal_ci(name, "self")) {
return scope->name;
} else {
return name;
}
}
static bool class_visible(zend_class_entry *ce) {
if (ce->type == ZEND_INTERNAL_CLASS) {
return !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES);
} else {
ZEND_ASSERT(ce->type == ZEND_USER_CLASS);
return !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
|| ce->info.user.filename == CG(compiled_filename);
}
}
static zend_always_inline void register_unresolved_class(zend_string *name) {
/* We'll autoload this class and process delayed variance obligations later. */
if (!CG(delayed_autoloads)) {
ALLOC_HASHTABLE(CG(delayed_autoloads));
zend_hash_init(CG(delayed_autoloads), 0, NULL, NULL, 0);
}
zend_hash_add_empty_element(CG(delayed_autoloads), name);
}
static zend_class_entry *lookup_class_ex(
zend_class_entry *scope, zend_string *name, bool register_unresolved) {
zend_class_entry *ce;
bool in_preload = CG(compiler_options) & ZEND_COMPILE_PRELOAD;
if (UNEXPECTED(!EG(active) && !in_preload)) {
zend_string *lc_name = zend_string_tolower(name);
ce = zend_hash_find_ptr(CG(class_table), lc_name);
zend_string_release(lc_name);
if (register_unresolved && !ce) {
zend_error_noreturn(
E_COMPILE_ERROR, "%s must be registered before %s",
ZSTR_VAL(name), ZSTR_VAL(scope->name));
}
return ce;
}
ce = zend_lookup_class_ex(
name, NULL, ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
if (!CG(in_compilation) || in_preload) {
if (ce) {
return ce;
}
if (register_unresolved) {
register_unresolved_class(name);
}
} else {
if (ce && class_visible(ce)) {
return ce;
}
/* The current class may not be registered yet, so check for it explicitly. */
if (zend_string_equals_ci(scope->name, name)) {
return scope;
}
}
return NULL;
}
static zend_class_entry *lookup_class(zend_class_entry *scope, zend_string *name) {
return lookup_class_ex(scope, name, /* register_unresolved */ false);
}
/* Instanceof that's safe to use on unlinked classes. */
static bool unlinked_instanceof(zend_class_entry *ce1, zend_class_entry *ce2) {
if (ce1 == ce2) {
return 1;
}
if (ce1->ce_flags & ZEND_ACC_LINKED) {
return instanceof_function(ce1, ce2);
}
if (ce1->parent) {
zend_class_entry *parent_ce;
if (ce1->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
parent_ce = ce1->parent;
} else {
parent_ce = zend_lookup_class_ex(ce1->parent_name, NULL,
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
}
/* It's not sufficient to only check the parent chain itself, as need to do a full
* recursive instanceof in case the parent interfaces haven't been copied yet. */
if (parent_ce && unlinked_instanceof(parent_ce, ce2)) {
return 1;
}
}
if (ce1->num_interfaces) {
uint32_t i;
if (ce1->ce_flags & ZEND_ACC_RESOLVED_INTERFACES) {
/* Unlike the normal instanceof_function(), we have to perform a recursive
* check here, as the parent interfaces might not have been fully copied yet. */
for (i = 0; i < ce1->num_interfaces; i++) {
if (unlinked_instanceof(ce1->interfaces[i], ce2)) {
return 1;
}
}
} else {
for (i = 0; i < ce1->num_interfaces; i++) {
zend_class_entry *ce = zend_lookup_class_ex(
ce1->interface_names[i].name, ce1->interface_names[i].lc_name,
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
/* Avoid recursing if class implements itself. */
if (ce && ce != ce1 && unlinked_instanceof(ce, ce2)) {
return 1;
}
}
}
}
return 0;
}
static bool zend_type_contains_traversable(zend_type type) {
zend_type *single_type;
if (ZEND_TYPE_FULL_MASK(type) & MAY_BE_OBJECT) {
return 1;
}
ZEND_TYPE_FOREACH(type, single_type) {
if (ZEND_TYPE_HAS_NAME(*single_type)
&& zend_string_equals_literal_ci(ZEND_TYPE_NAME(*single_type), "Traversable")) {
return 1;
}
} ZEND_TYPE_FOREACH_END();
return 0;
}
static bool zend_type_permits_self(
zend_type type, zend_class_entry *scope, zend_class_entry *self) {
if (ZEND_TYPE_FULL_MASK(type) & MAY_BE_OBJECT) {
return 1;
}
/* Any types that may satisfy self must have already been loaded at this point
* (as a parent or interface), so we never need to register delayed variance obligations
* for this case. */
zend_type *single_type;
ZEND_TYPE_FOREACH(type, single_type) {
if (ZEND_TYPE_HAS_NAME(*single_type)) {
zend_string *name = resolve_class_name(scope, ZEND_TYPE_NAME(*single_type));
zend_class_entry *ce = lookup_class(self, name);
if (ce && unlinked_instanceof(self, ce)) {
return 1;
}
}
} ZEND_TYPE_FOREACH_END();
return 0;
}
static void track_class_dependency(zend_class_entry *ce, zend_string *class_name)
{
HashTable *ht;
if (!CG(current_linking_class) || ce == CG(current_linking_class)) {
return;
} else if (!class_name) {
class_name = ce->name;
} else if (zend_string_equals_literal_ci(class_name, "self")
|| zend_string_equals_literal_ci(class_name, "parent")) {
return;
}
#ifndef ZEND_WIN32
/* On non-Windows systems, internal classes are always the same,
* so there is no need to explicitly track them. */
if (ce->type == ZEND_INTERNAL_CLASS) {
return;
}
#endif
ht = (HashTable*)CG(current_linking_class)->inheritance_cache;
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
// TODO: dependency on not-immutable class ???
if (ht) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
CG(current_linking_class)->inheritance_cache = NULL;
}
CG(current_linking_class)->ce_flags &= ~ZEND_ACC_CACHEABLE;
CG(current_linking_class) = NULL;
return;
}
/* Record dependency */
if (!ht) {
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 0, NULL, NULL, 0);
CG(current_linking_class)->inheritance_cache = (zend_inheritance_cache_entry*)ht;
}
zend_hash_add_ptr(ht, class_name, ce);
}
/* Check whether any type in the fe_type intersection type is a subtype of the proto class. */
static inheritance_status zend_is_intersection_subtype_of_class(
zend_class_entry *fe_scope, zend_type fe_type,
zend_class_entry *proto_scope, zend_string *proto_class_name, zend_class_entry *proto_ce)
{
ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(fe_type));
bool have_unresolved = false;
zend_type *single_type;
/* Traverse the list of child types and check that at least one is
* a subtype of the parent type being checked */
ZEND_TYPE_FOREACH(fe_type, single_type) {
zend_class_entry *fe_ce;
zend_string *fe_class_name = NULL;
if (ZEND_TYPE_HAS_NAME(*single_type)) {
fe_class_name =
resolve_class_name(fe_scope, ZEND_TYPE_NAME(*single_type));
if (zend_string_equals_ci(fe_class_name, proto_class_name)) {
return INHERITANCE_SUCCESS;
}
if (!proto_ce) proto_ce = lookup_class(proto_scope, proto_class_name);
fe_ce = lookup_class(fe_scope, fe_class_name);
} else {
/* standard type in an intersection type is impossible,
* because it would be a fatal compile error */
ZEND_UNREACHABLE();
continue;
}
if (!fe_ce || !proto_ce) {
have_unresolved = true;
continue;
}
if (unlinked_instanceof(fe_ce, proto_ce)) {
track_class_dependency(fe_ce, fe_class_name);
track_class_dependency(proto_ce, proto_class_name);
return INHERITANCE_SUCCESS;
}
} ZEND_TYPE_FOREACH_END();
return have_unresolved ? INHERITANCE_UNRESOLVED : INHERITANCE_ERROR;
}
/* Check whether a single class proto type is a subtype of a potentially complex fe_type. */
static inheritance_status zend_is_class_subtype_of_type(
zend_class_entry *fe_scope, zend_string *fe_class_name,
zend_class_entry *proto_scope, zend_type proto_type) {
zend_class_entry *fe_ce = NULL;
bool have_unresolved = 0;
/* If the parent has 'object' as a return type, any class satisfies the co-variant check */
if (ZEND_TYPE_FULL_MASK(proto_type) & MAY_BE_OBJECT) {
/* Currently, any class name would be allowed here. We still perform a class lookup
* for forward-compatibility reasons, as we may have named types in the future that
* are not classes (such as typedefs). */
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
if (!fe_ce) {
have_unresolved = 1;
} else {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
}
}
if (ZEND_TYPE_FULL_MASK(proto_type) & MAY_BE_ITERABLE) {
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
if (!fe_ce) {
have_unresolved = 1;
} else if (unlinked_instanceof(fe_ce, zend_ce_traversable)) {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
}
}
zend_type *single_type;
/* Traverse the list of parent types and check if the current child (FE)
* class is the subtype of at least one of them (union) or all of them (intersection). */
bool is_intersection = ZEND_TYPE_IS_INTERSECTION(proto_type);
ZEND_TYPE_FOREACH(proto_type, single_type) {
zend_class_entry *proto_ce;
zend_string *proto_class_name = NULL;
if (ZEND_TYPE_HAS_NAME(*single_type)) {
proto_class_name =
resolve_class_name(proto_scope, ZEND_TYPE_NAME(*single_type));
if (zend_string_equals_ci(fe_class_name, proto_class_name)) {
if (!is_intersection) {
return INHERITANCE_SUCCESS;
}
continue;
}
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
proto_ce = lookup_class(proto_scope, proto_class_name);
} else {
/* standard type */
ZEND_ASSERT(!is_intersection);
continue;
}
if (!fe_ce || !proto_ce) {
have_unresolved = 1;
continue;
}
if (unlinked_instanceof(fe_ce, proto_ce)) {
track_class_dependency(fe_ce, fe_class_name);
track_class_dependency(proto_ce, proto_class_name);
if (!is_intersection) {
return INHERITANCE_SUCCESS;
}
} else {
if (is_intersection) {
return INHERITANCE_ERROR;
}
}
} ZEND_TYPE_FOREACH_END();
if (have_unresolved) {
return INHERITANCE_UNRESOLVED;
}
return is_intersection ? INHERITANCE_SUCCESS : INHERITANCE_ERROR;
}
static zend_string *get_class_from_type(zend_class_entry *scope, zend_type single_type) {
if (ZEND_TYPE_HAS_NAME(single_type)) {
return resolve_class_name(scope, ZEND_TYPE_NAME(single_type));
}
return NULL;
}
static void register_unresolved_classes(zend_class_entry *scope, zend_type type) {
zend_type *single_type;
ZEND_TYPE_FOREACH(type, single_type) {
if (ZEND_TYPE_HAS_NAME(*single_type)) {
zend_string *class_name = resolve_class_name(scope, ZEND_TYPE_NAME(*single_type));
lookup_class_ex(scope, class_name, /* register_unresolved */ true);
}
} ZEND_TYPE_FOREACH_END();
}
static inheritance_status zend_perform_covariant_type_check(
zend_class_entry *fe_scope, zend_type fe_type,
zend_class_entry *proto_scope, zend_type proto_type)
{
ZEND_ASSERT(ZEND_TYPE_IS_SET(fe_type) && ZEND_TYPE_IS_SET(proto_type));
/* Apart from void, everything is trivially covariant to the mixed type.
* Handle this case separately to ensure it never requires class loading. */
if (ZEND_TYPE_PURE_MASK(proto_type) == MAY_BE_ANY &&
!ZEND_TYPE_CONTAINS_CODE(fe_type, IS_VOID)) {
return INHERITANCE_SUCCESS;
}
/* Builtin types may be removed, but not added */
uint32_t fe_type_mask = ZEND_TYPE_PURE_MASK(fe_type);
uint32_t proto_type_mask = ZEND_TYPE_PURE_MASK(proto_type);
uint32_t added_types = fe_type_mask & ~proto_type_mask;
if (added_types) {
// TODO: Make "iterable" an alias of "array|Traversable" instead,
// so these special cases will be handled automatically.
if ((added_types & MAY_BE_ITERABLE)
&& (proto_type_mask & MAY_BE_ARRAY)
&& zend_type_contains_traversable(proto_type)) {
/* Replacing array|Traversable with iterable is okay */
added_types &= ~MAY_BE_ITERABLE;
}
if ((added_types & MAY_BE_ARRAY) && (proto_type_mask & MAY_BE_ITERABLE)) {
/* Replacing iterable with array is okay */
added_types &= ~MAY_BE_ARRAY;
}
if ((added_types & MAY_BE_STATIC)
&& zend_type_permits_self(proto_type, proto_scope, fe_scope)) {
/* Replacing type that accepts self with static is okay */
added_types &= ~MAY_BE_STATIC;
}
if (added_types == MAY_BE_NEVER) {
/* never is the bottom type */
return INHERITANCE_SUCCESS;
}
if (added_types) {
/* Otherwise adding new types is illegal */
return INHERITANCE_ERROR;
}
}
zend_type *single_type;
inheritance_status early_exit_status;
bool have_unresolved = false;
if (ZEND_TYPE_IS_INTERSECTION(fe_type)) {
/* Currently, for object type any class name would be allowed here.
* We still perform a class lookup for forward-compatibility reasons,
* as we may have named types in the future that are not classes
* (such as typedefs). */
if (proto_type_mask & (MAY_BE_OBJECT|MAY_BE_ITERABLE)) {
bool any_class = (proto_type_mask & MAY_BE_OBJECT) != 0;
ZEND_TYPE_FOREACH(fe_type, single_type) {
zend_string *fe_class_name = get_class_from_type(fe_scope, *single_type);
if (!fe_class_name) {
continue;
}
zend_class_entry *fe_ce = lookup_class(fe_scope, fe_class_name);
if (fe_ce) {
if (any_class || unlinked_instanceof(fe_ce, zend_ce_traversable)) {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
}
} else {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
}
/* U_1&...&U_n < V_1&...&V_m if forall V_j. exists U_i. U_i < V_j.
* U_1&...&U_n < V_1|...|V_m if exists V_j. exists U_i. U_i < V_j.
* As such, we need to iterate over proto_type (V_j) first and use a different
* quantifier depending on whether fe_type is a union or an intersection. */
early_exit_status =
ZEND_TYPE_IS_INTERSECTION(proto_type) ? INHERITANCE_ERROR : INHERITANCE_SUCCESS;
ZEND_TYPE_FOREACH(proto_type, single_type) {
zend_string *proto_class_name = get_class_from_type(proto_scope, *single_type);
if (!proto_class_name) {
continue;
}
zend_class_entry *proto_ce = NULL;
inheritance_status status = zend_is_intersection_subtype_of_class(
fe_scope, fe_type, proto_scope, proto_class_name, proto_ce);
if (status == early_exit_status) {
return status;
}
if (status == INHERITANCE_UNRESOLVED) {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
} else {
/* U_1|...|U_n < V_1|...|V_m if forall U_i. exists V_j. U_i < V_j.
* U_1|...|U_n < V_1&...&V_m if forall U_i. forall V_j. U_i < V_j.
* We need to iterate over fe_type (U_i) first and the logic is independent of
* whether proto_type is a union or intersection (only the inner check differs). */
early_exit_status = INHERITANCE_ERROR;
ZEND_TYPE_FOREACH(fe_type, single_type) {
zend_string *fe_class_name = get_class_from_type(fe_scope, *single_type);
if (!fe_class_name) {
continue;
}
inheritance_status status = zend_is_class_subtype_of_type(
fe_scope, fe_class_name, proto_scope, proto_type);
if (status == early_exit_status) {
return status;
}
if (status == INHERITANCE_UNRESOLVED) {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
}
if (!have_unresolved) {
return early_exit_status == INHERITANCE_ERROR ? INHERITANCE_SUCCESS : INHERITANCE_ERROR;
}
register_unresolved_classes(fe_scope, fe_type);
register_unresolved_classes(proto_scope, proto_type);
return INHERITANCE_UNRESOLVED;
}
static inheritance_status zend_do_perform_arg_type_hint_check(
zend_class_entry *fe_scope, zend_arg_info *fe_arg_info,
zend_class_entry *proto_scope, zend_arg_info *proto_arg_info) /* {{{ */
{
if (!ZEND_TYPE_IS_SET(fe_arg_info->type) || ZEND_TYPE_PURE_MASK(fe_arg_info->type) == MAY_BE_ANY) {
/* Child with no type or mixed type is always compatible */
return INHERITANCE_SUCCESS;
}
if (!ZEND_TYPE_IS_SET(proto_arg_info->type)) {
/* Child defines a type, but parent doesn't, violates LSP */
return INHERITANCE_ERROR;
}
/* Contravariant type check is performed as a covariant type check with swapped
* argument order. */
return zend_perform_covariant_type_check(
proto_scope, proto_arg_info->type, fe_scope, fe_arg_info->type);
}
/* }}} */
/* For trait methods, fe_scope/proto_scope may differ from fe/proto->common.scope,
* as self will refer to the self of the class the trait is used in, not the trait
* the method was declared in. */
static inheritance_status zend_do_perform_implementation_check(
const zend_function *fe, zend_class_entry *fe_scope,
const zend_function *proto, zend_class_entry *proto_scope) /* {{{ */
{
uint32_t i, num_args, proto_num_args, fe_num_args;
inheritance_status status, local_status;
bool proto_is_variadic, fe_is_variadic;
/* Checks for constructors only if they are declared in an interface,
* or explicitly marked as abstract
*/
ZEND_ASSERT(!((fe->common.fn_flags & ZEND_ACC_CTOR)
&& ((proto->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0
&& (proto->common.fn_flags & ZEND_ACC_ABSTRACT) == 0)));
/* If the prototype method is private and not abstract, we do not enforce a signature.
* private abstract methods can only occur in traits. */
ZEND_ASSERT(!(proto->common.fn_flags & ZEND_ACC_PRIVATE)
|| (proto->common.fn_flags & ZEND_ACC_ABSTRACT));
/* The number of required arguments cannot increase. */
if (proto->common.required_num_args < fe->common.required_num_args) {
return INHERITANCE_ERROR;
}
/* by-ref constraints on return values are covariant */
if ((proto->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)
&& !(fe->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
return INHERITANCE_ERROR;
}
proto_is_variadic = (proto->common.fn_flags & ZEND_ACC_VARIADIC) != 0;
fe_is_variadic = (fe->common.fn_flags & ZEND_ACC_VARIADIC) != 0;
/* A variadic function cannot become non-variadic */
if (proto_is_variadic && !fe_is_variadic) {
return INHERITANCE_ERROR;
}
/* The variadic argument is not included in the stored argument count. */
proto_num_args = proto->common.num_args + proto_is_variadic;
fe_num_args = fe->common.num_args + fe_is_variadic;
num_args = MAX(proto_num_args, fe_num_args);
status = INHERITANCE_SUCCESS;
for (i = 0; i < num_args; i++) {
zend_arg_info *proto_arg_info =
i < proto_num_args ? &proto->common.arg_info[i] :
proto_is_variadic ? &proto->common.arg_info[proto_num_args - 1] : NULL;
zend_arg_info *fe_arg_info =
i < fe_num_args ? &fe->common.arg_info[i] :
fe_is_variadic ? &fe->common.arg_info[fe_num_args - 1] : NULL;
if (!proto_arg_info) {
/* A new (optional) argument has been added, which is fine. */
continue;
}
if (!fe_arg_info) {
/* An argument has been removed. This is considered illegal, because arity checks
* work based on a model where passing more than the declared number of parameters
* to a function is an error. */
return INHERITANCE_ERROR;
}
local_status = zend_do_perform_arg_type_hint_check(
fe_scope, fe_arg_info, proto_scope, proto_arg_info);
if (UNEXPECTED(local_status != INHERITANCE_SUCCESS)) {
if (UNEXPECTED(local_status == INHERITANCE_ERROR)) {
return INHERITANCE_ERROR;
}
ZEND_ASSERT(local_status == INHERITANCE_UNRESOLVED);
status = INHERITANCE_UNRESOLVED;
}
/* by-ref constraints on arguments are invariant */
if (ZEND_ARG_SEND_MODE(fe_arg_info) != ZEND_ARG_SEND_MODE(proto_arg_info)) {
return INHERITANCE_ERROR;
}
}
/* Check return type compatibility, but only if the prototype already specifies
* a return type. Adding a new return type is always valid. */
if (proto->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
/* Removing a return type is not valid, unless the parent return type is tentative. */
if (!(fe->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
if (!ZEND_ARG_TYPE_IS_TENTATIVE(&proto->common.arg_info[-1])) {
return INHERITANCE_ERROR;
}
if (status == INHERITANCE_SUCCESS) {
return INHERITANCE_WARNING;
}
return status;
}
local_status = zend_perform_covariant_type_check(
fe_scope, fe->common.arg_info[-1].type, proto_scope, proto->common.arg_info[-1].type);
if (UNEXPECTED(local_status != INHERITANCE_SUCCESS)) {
if (local_status == INHERITANCE_ERROR
&& ZEND_ARG_TYPE_IS_TENTATIVE(&proto->common.arg_info[-1])) {
local_status = INHERITANCE_WARNING;
}
return local_status;
}
}
return status;
}
/* }}} */
static ZEND_COLD void zend_append_type_hint(
smart_str *str, zend_class_entry *scope, zend_arg_info *arg_info, bool return_hint) /* {{{ */
{
if (ZEND_TYPE_IS_SET(arg_info->type)) {
zend_string *type_str = zend_type_to_string_resolved(arg_info->type, scope);
smart_str_append(str, type_str);
zend_string_release(type_str);
if (!return_hint) {
smart_str_appendc(str, ' ');
}
}
}
/* }}} */
static ZEND_COLD zend_string *zend_get_function_declaration(
const zend_function *fptr, zend_class_entry *scope) /* {{{ */
{
smart_str str = {0};
if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
smart_str_appends(&str, "& ");
}
if (fptr->common.scope) {
if (fptr->common.scope->ce_flags & ZEND_ACC_ANON_CLASS) {
/* cut off on NULL byte ... class@anonymous */
smart_str_appends(&str, ZSTR_VAL(fptr->common.scope->name));
} else {
smart_str_appendl(&str, ZSTR_VAL(fptr->common.scope->name), ZSTR_LEN(fptr->common.scope->name));
}
smart_str_appends(&str, "::");
}
smart_str_append(&str, fptr->common.function_name);
smart_str_appendc(&str, '(');
if (fptr->common.arg_info) {
uint32_t i, num_args, required;
zend_arg_info *arg_info = fptr->common.arg_info;
required = fptr->common.required_num_args;
num_args = fptr->common.num_args;
if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
for (i = 0; i < num_args;) {
zend_append_type_hint(&str, scope, arg_info, 0);
if (ZEND_ARG_SEND_MODE(arg_info)) {
smart_str_appendc(&str, '&');
}
if (ZEND_ARG_IS_VARIADIC(arg_info)) {
smart_str_appends(&str, "...");
}
smart_str_appendc(&str, '$');
if (fptr->type == ZEND_INTERNAL_FUNCTION) {
smart_str_appends(&str, ((zend_internal_arg_info*)arg_info)->name);
} else {
smart_str_appendl(&str, ZSTR_VAL(arg_info->name), ZSTR_LEN(arg_info->name));
}
if (i >= required && !ZEND_ARG_IS_VARIADIC(arg_info)) {
smart_str_appends(&str, " = ");
if (fptr->type == ZEND_INTERNAL_FUNCTION) {
if (((zend_internal_arg_info*)arg_info)->default_value) {
smart_str_appends(&str, ((zend_internal_arg_info*)arg_info)->default_value);
} else {
smart_str_appends(&str, "<default>");
}
} else {
zend_op *precv = NULL;
{
uint32_t idx = i;
zend_op *op = fptr->op_array.opcodes;
zend_op *end = op + fptr->op_array.last;
++idx;
while (op < end) {
if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT)
&& op->op1.num == (zend_ulong)idx)
{
precv = op;
}
++op;
}
}
if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) {
zval *zv = RT_CONSTANT(precv, precv->op2);
if (Z_TYPE_P(zv) == IS_FALSE) {
smart_str_appends(&str, "false");
} else if (Z_TYPE_P(zv) == IS_TRUE) {
smart_str_appends(&str, "true");
} else if (Z_TYPE_P(zv) == IS_NULL) {
smart_str_appends(&str, "null");
} else if (Z_TYPE_P(zv) == IS_STRING) {
smart_str_appendc(&str, '\'');
smart_str_appendl(&str, Z_STRVAL_P(zv), MIN(Z_STRLEN_P(zv), 10));
if (Z_STRLEN_P(zv) > 10) {
smart_str_appends(&str, "...");
}
smart_str_appendc(&str, '\'');
} else if (Z_TYPE_P(zv) == IS_ARRAY) {
if (zend_hash_num_elements(Z_ARRVAL_P(zv)) == 0) {
smart_str_appends(&str, "[]");
} else {
smart_str_appends(&str, "[...]");
}
} else if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
zend_ast *ast = Z_ASTVAL_P(zv);
if (ast->kind == ZEND_AST_CONSTANT) {
smart_str_append(&str, zend_ast_get_constant_name(ast));
} else if (ast->kind == ZEND_AST_CLASS_CONST) {
smart_str_append(&str, zend_ast_get_str(ast->child[0]));
smart_str_appends(&str, "::");
smart_str_append(&str, zend_ast_get_str(ast->child[1]));
} else {
smart_str_appends(&str, "<expression>");
}
} else {
zend_string *tmp_zv_str;
zend_string *zv_str = zval_get_tmp_string(zv, &tmp_zv_str);
smart_str_append(&str, zv_str);
zend_tmp_string_release(tmp_zv_str);
}
}
}
}
if (++i < num_args) {
smart_str_appends(&str, ", ");
}
arg_info++;
}
}
smart_str_appendc(&str, ')');
if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
smart_str_appends(&str, ": ");
zend_append_type_hint(&str, scope, fptr->common.arg_info - 1, 1);
}
smart_str_0(&str);
return str.s;
}
/* }}} */
static zend_always_inline zend_string *func_filename(const zend_function *fn) {
return fn->common.type == ZEND_USER_FUNCTION ? fn->op_array.filename : NULL;
}
static zend_always_inline uint32_t func_lineno(const zend_function *fn) {
return fn->common.type == ZEND_USER_FUNCTION ? fn->op_array.line_start : 0;
}
static void ZEND_COLD emit_incompatible_method_error(
const zend_function *child, zend_class_entry *child_scope,
const zend_function *parent, zend_class_entry *parent_scope,
inheritance_status status) {
zend_string *parent_prototype = zend_get_function_declaration(parent, parent_scope);
zend_string *child_prototype = zend_get_function_declaration(child, child_scope);
if (status == INHERITANCE_UNRESOLVED) {
/* Fetch the first unresolved class from registered autoloads */
zend_string *unresolved_class = NULL;
ZEND_HASH_FOREACH_STR_KEY(CG(delayed_autoloads), unresolved_class) {
break;
} ZEND_HASH_FOREACH_END();
ZEND_ASSERT(unresolved_class);
zend_error_at(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Could not check compatibility between %s and %s, because class %s is not available",
ZSTR_VAL(child_prototype), ZSTR_VAL(parent_prototype), ZSTR_VAL(unresolved_class));
} else if (status == INHERITANCE_WARNING) {
zend_attribute *return_type_will_change_attribute = zend_get_attribute_str(
child->common.attributes,
"returntypewillchange",
sizeof("returntypewillchange")-1
);
if (!return_type_will_change_attribute) {
zend_error_at(E_DEPRECATED, func_filename(child), func_lineno(child),
"Return type of %s should either be compatible with %s, "