forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zend_execute.c
4906 lines (4356 loc) · 149 KB
/
zend_execute.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]> |
| Dmitry Stogov <[email protected]> |
+----------------------------------------------------------------------+
*/
#define ZEND_INTENSIVE_DEBUGGING 0
#include <stdio.h>
#include <signal.h>
#include "zend.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_API.h"
#include "zend_ptr_stack.h"
#include "zend_constants.h"
#include "zend_extensions.h"
#include "zend_ini.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "zend_generators.h"
#include "zend_vm.h"
#include "zend_dtrace.h"
#include "zend_inheritance.h"
#include "zend_type_info.h"
#include "zend_smart_str.h"
#include "zend_observer.h"
#include "zend_system_id.h"
#include "Optimizer/zend_func_info.h"
/* Virtual current working directory support */
#include "zend_virtual_cwd.h"
#ifdef HAVE_GCC_GLOBAL_REGS
# if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
# define ZEND_VM_FP_GLOBAL_REG "%esi"
# define ZEND_VM_IP_GLOBAL_REG "%edi"
# elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
# define ZEND_VM_FP_GLOBAL_REG "%r14"
# define ZEND_VM_IP_GLOBAL_REG "%r15"
# elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
# define ZEND_VM_FP_GLOBAL_REG "r14"
# define ZEND_VM_IP_GLOBAL_REG "r15"
# elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
# define ZEND_VM_FP_GLOBAL_REG "r14"
# define ZEND_VM_IP_GLOBAL_REG "r15"
# elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
# define ZEND_VM_FP_GLOBAL_REG "x27"
# define ZEND_VM_IP_GLOBAL_REG "x28"
# endif
#endif
#if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
# pragma GCC diagnostic ignored "-Wvolatile-register-var"
register zend_execute_data* volatile execute_data __asm__(ZEND_VM_FP_GLOBAL_REG);
# pragma GCC diagnostic warning "-Wvolatile-register-var"
#endif
#if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
# define EXECUTE_DATA_D void
# define EXECUTE_DATA_C
# define EXECUTE_DATA_DC
# define EXECUTE_DATA_CC
# define NO_EXECUTE_DATA_CC
#else
# define EXECUTE_DATA_D zend_execute_data* execute_data
# define EXECUTE_DATA_C execute_data
# define EXECUTE_DATA_DC , EXECUTE_DATA_D
# define EXECUTE_DATA_CC , EXECUTE_DATA_C
# define NO_EXECUTE_DATA_CC , NULL
#endif
#if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
# define OPLINE_D void
# define OPLINE_C
# define OPLINE_DC
# define OPLINE_CC
#else
# define OPLINE_D const zend_op* opline
# define OPLINE_C opline
# define OPLINE_DC , OPLINE_D
# define OPLINE_CC , OPLINE_C
#endif
#if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
# pragma GCC diagnostic ignored "-Wvolatile-register-var"
register const zend_op* volatile opline __asm__(ZEND_VM_IP_GLOBAL_REG);
# pragma GCC diagnostic warning "-Wvolatile-register-var"
#else
#endif
#define _CONST_CODE 0
#define _TMP_CODE 1
#define _VAR_CODE 2
#define _UNUSED_CODE 3
#define _CV_CODE 4
typedef int (ZEND_FASTCALL *incdec_t)(zval *);
#define get_zval_ptr(op_type, node, type) _get_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
#define get_zval_ptr_deref(op_type, node, type) _get_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
#define get_zval_ptr_undef(op_type, node, type) _get_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
#define get_op_data_zval_ptr_r(op_type, node) _get_op_data_zval_ptr_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
#define get_op_data_zval_ptr_deref_r(op_type, node) _get_op_data_zval_ptr_deref_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
#define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
#define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
#define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
#define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
#define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
#define RETURN_VALUE_USED(opline) ((opline)->result_type != IS_UNUSED)
static ZEND_FUNCTION(pass)
{
}
ZEND_BEGIN_ARG_INFO_EX(zend_pass_function_arg_info, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_API const zend_internal_function zend_pass_function = {
ZEND_INTERNAL_FUNCTION, /* type */
{0, 0, 0}, /* arg_flags */
0, /* fn_flags */
NULL, /* name */
NULL, /* scope */
NULL, /* prototype */
0, /* num_args */
0, /* required_num_args */
(zend_internal_arg_info *) zend_pass_function_arg_info + 1, /* arg_info */
NULL, /* attributes */
ZEND_FN(pass), /* handler */
NULL, /* module */
{NULL,NULL,NULL,NULL} /* reserved */
};
#define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_var) do { \
zval *__container_to_free = EX_VAR(free_var); \
if (UNEXPECTED(Z_REFCOUNTED_P(__container_to_free))) { \
zend_refcounted *__ref = Z_COUNTED_P(__container_to_free); \
if (UNEXPECTED(!GC_DELREF(__ref))) { \
zval *__zv = EX_VAR(opline->result.var); \
if (EXPECTED(Z_TYPE_P(__zv) == IS_INDIRECT)) { \
ZVAL_COPY(__zv, Z_INDIRECT_P(__zv)); \
} \
rc_dtor_func(__ref); \
} \
} \
} while (0)
#define FREE_OP(type, var) \
if ((type) & (IS_TMP_VAR|IS_VAR)) { \
zval_ptr_dtor_nogc(EX_VAR(var)); \
}
#define CV_DEF_OF(i) (EX(func)->op_array.vars[i])
#define ZEND_VM_STACK_PAGE_SLOTS (16 * 1024) /* should be a power of 2 */
#define ZEND_VM_STACK_PAGE_SIZE (ZEND_VM_STACK_PAGE_SLOTS * sizeof(zval))
#define ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, page_size) \
(((size) + ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval) \
+ ((page_size) - 1)) & ~((page_size) - 1))
ZEND_API void zend_vm_stack_init(void)
{
EG(vm_stack_page_size) = ZEND_VM_STACK_PAGE_SIZE;
EG(vm_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE, NULL);
EG(vm_stack_top) = EG(vm_stack)->top;
EG(vm_stack_end) = EG(vm_stack)->end;
}
ZEND_API void zend_vm_stack_init_ex(size_t page_size)
{
/* page_size must be a power of 2 */
ZEND_ASSERT(page_size > 0 && (page_size & (page_size - 1)) == 0);
EG(vm_stack_page_size) = page_size;
EG(vm_stack) = zend_vm_stack_new_page(page_size, NULL);
EG(vm_stack_top) = EG(vm_stack)->top;
EG(vm_stack_end) = EG(vm_stack)->end;
}
ZEND_API void zend_vm_stack_destroy(void)
{
zend_vm_stack stack = EG(vm_stack);
while (stack != NULL) {
zend_vm_stack p = stack->prev;
efree(stack);
stack = p;
}
}
ZEND_API void* zend_vm_stack_extend(size_t size)
{
zend_vm_stack stack;
void *ptr;
stack = EG(vm_stack);
stack->top = EG(vm_stack_top);
EG(vm_stack) = stack = zend_vm_stack_new_page(
EXPECTED(size < EG(vm_stack_page_size) - (ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval))) ?
EG(vm_stack_page_size) : ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, EG(vm_stack_page_size)),
stack);
ptr = stack->top;
EG(vm_stack_top) = (void*)(((char*)ptr) + size);
EG(vm_stack_end) = stack->end;
return ptr;
}
ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data, uint32_t var)
{
return EX_VAR(var);
}
static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE);
return ret;
}
static zend_always_inline zval *_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
return ret;
}
static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
ZVAL_DEREF(ret);
return ret;
}
static zend_never_inline ZEND_COLD zval* zval_undefined_cv(uint32_t var EXECUTE_DATA_DC)
{
if (EXPECTED(EG(exception) == NULL)) {
zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
zend_error(E_WARNING, "Undefined variable $%s", ZSTR_VAL(cv));
}
return &EG(uninitialized_zval);
}
static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op1(EXECUTE_DATA_D)
{
return zval_undefined_cv(EX(opline)->op1.var EXECUTE_DATA_CC);
}
static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op2(EXECUTE_DATA_D)
{
return zval_undefined_cv(EX(opline)->op2.var EXECUTE_DATA_CC);
}
#define ZVAL_UNDEFINED_OP1() _zval_undefined_op1(EXECUTE_DATA_C)
#define ZVAL_UNDEFINED_OP2() _zval_undefined_op2(EXECUTE_DATA_C)
static zend_never_inline ZEND_COLD zval *_get_zval_cv_lookup(zval *ptr, uint32_t var, int type EXECUTE_DATA_DC)
{
switch (type) {
case BP_VAR_R:
case BP_VAR_UNSET:
ptr = zval_undefined_cv(var EXECUTE_DATA_CC);
break;
case BP_VAR_IS:
ptr = &EG(uninitialized_zval);
break;
case BP_VAR_RW:
zval_undefined_cv(var EXECUTE_DATA_CC);
ZEND_FALLTHROUGH;
case BP_VAR_W:
ZVAL_NULL(ptr);
break;
}
return ptr;
}
static zend_always_inline zval *_get_zval_ptr_cv(uint32_t var, int type EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
if (type == BP_VAR_W) {
ZVAL_NULL(ret);
} else {
return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
}
}
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_deref(uint32_t var, int type EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
if (type == BP_VAR_W) {
ZVAL_NULL(ret);
return ret;
} else {
return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
}
}
ZVAL_DEREF(ret);
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
return zval_undefined_cv(var EXECUTE_DATA_CC);
}
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
return zval_undefined_cv(var EXECUTE_DATA_CC);
}
ZVAL_DEREF(ret);
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_IS(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_RW(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
zval_undefined_cv(var EXECUTE_DATA_CC);
ZVAL_NULL(ret);
return ret;
}
return ret;
}
static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (Z_TYPE_P(ret) == IS_UNDEF) {
ZVAL_NULL(ret);
}
return ret;
}
static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type & (IS_TMP_VAR|IS_VAR)) {
if (!ZEND_DEBUG || op_type == IS_VAR) {
return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
} else {
ZEND_ASSERT(op_type == IS_TMP_VAR);
return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
}
} else {
if (op_type == IS_CONST) {
return RT_CONSTANT(opline, node);
} else if (op_type == IS_CV) {
return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
} else {
return NULL;
}
}
}
static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type & (IS_TMP_VAR|IS_VAR)) {
if (!ZEND_DEBUG || op_type == IS_VAR) {
return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
} else {
ZEND_ASSERT(op_type == IS_TMP_VAR);
return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
}
} else {
if (op_type == IS_CONST) {
return RT_CONSTANT(opline + 1, node);
} else if (op_type == IS_CV) {
return _get_zval_ptr_cv_BP_VAR_R(node.var EXECUTE_DATA_CC);
} else {
return NULL;
}
}
}
static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type & (IS_TMP_VAR|IS_VAR)) {
if (op_type == IS_TMP_VAR) {
return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
} else {
ZEND_ASSERT(op_type == IS_VAR);
return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
}
} else {
if (op_type == IS_CONST) {
return RT_CONSTANT(opline, node);
} else if (op_type == IS_CV) {
return _get_zval_ptr_cv_deref(node.var, type EXECUTE_DATA_CC);
} else {
return NULL;
}
}
}
static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_deref_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type & (IS_TMP_VAR|IS_VAR)) {
if (op_type == IS_TMP_VAR) {
return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
} else {
ZEND_ASSERT(op_type == IS_VAR);
return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
}
} else {
if (op_type == IS_CONST) {
return RT_CONSTANT(opline + 1, node);
} else if (op_type == IS_CV) {
return _get_zval_ptr_cv_deref_BP_VAR_R(node.var EXECUTE_DATA_CC);
} else {
return NULL;
}
}
}
static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type & (IS_TMP_VAR|IS_VAR)) {
if (!ZEND_DEBUG || op_type == IS_VAR) {
return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
} else {
ZEND_ASSERT(op_type == IS_TMP_VAR);
return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
}
} else {
if (op_type == IS_CONST) {
return RT_CONSTANT(opline, node);
} else if (op_type == IS_CV) {
return EX_VAR(node.var);
} else {
return NULL;
}
}
}
static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC)
{
zval *ret = EX_VAR(var);
if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) {
ret = Z_INDIRECT_P(ret);
}
return ret;
}
static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
{
if (op_type == IS_CV) {
return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
} else /* if (op_type == IS_VAR) */ {
ZEND_ASSERT(op_type == IS_VAR);
return _get_zval_ptr_ptr_var(node.var EXECUTE_DATA_CC);
}
}
static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type == IS_UNUSED) {
return &EX(This);
}
return get_zval_ptr(op_type, op, type);
}
static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
{
if (op_type == IS_UNUSED) {
return &EX(This);
}
return get_zval_ptr_undef(op_type, op, type);
}
static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
{
if (op_type == IS_UNUSED) {
return &EX(This);
}
return get_zval_ptr_ptr(op_type, node, type);
}
static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr)
{
zend_reference *ref;
if (EXPECTED(!Z_ISREF_P(value_ptr))) {
ZVAL_NEW_REF(value_ptr, value_ptr);
} else if (UNEXPECTED(variable_ptr == value_ptr)) {
return;
}
ref = Z_REF_P(value_ptr);
GC_ADDREF(ref);
if (Z_REFCOUNTED_P(variable_ptr)) {
zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
if (GC_DELREF(garbage) == 0) {
ZVAL_REF(variable_ptr, ref);
rc_dtor_func(garbage);
return;
} else {
gc_check_possible_root(garbage);
}
}
ZVAL_REF(variable_ptr, ref);
}
static zend_never_inline zval* zend_assign_to_typed_property_reference(zend_property_info *prop_info, zval *prop, zval *value_ptr EXECUTE_DATA_DC)
{
if (!zend_verify_prop_assignable_by_ref(prop_info, value_ptr, EX_USES_STRICT_TYPES())) {
return &EG(uninitialized_zval);
}
if (Z_ISREF_P(prop)) {
ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(prop), prop_info);
}
zend_assign_to_variable_reference(prop, value_ptr);
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(prop), prop_info);
return prop;
}
static zend_never_inline ZEND_COLD zval *zend_wrong_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
{
zend_error(E_NOTICE, "Only variables should be assigned by reference");
if (UNEXPECTED(EG(exception) != NULL)) {
return &EG(uninitialized_zval);
}
/* Use IS_TMP_VAR instead of IS_VAR to avoid ISREF check */
Z_TRY_ADDREF_P(value_ptr);
return zend_assign_to_variable(variable_ptr, value_ptr, IS_TMP_VAR, EX_USES_STRICT_TYPES());
}
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_cannot_pass_by_reference(uint32_t arg_num)
{
const zend_execute_data *execute_data = EG(current_execute_data);
zend_string *func_name = get_function_or_method_name(EX(call)->func);
const char *param_name = get_function_arg_name(EX(call)->func, arg_num);
zend_throw_error(NULL, "%s(): Argument #%d%s%s%s cannot be passed by reference",
ZSTR_VAL(func_name), arg_num, param_name ? " ($" : "", param_name ? param_name : "", param_name ? ")" : ""
);
zend_string_release(func_name);
}
static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_prop_error(zend_property_info *prop) {
zend_string *type_str = zend_type_to_string(prop->type);
zend_type_error(
"Cannot auto-initialize an array inside property %s::$%s of type %s",
ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
ZSTR_VAL(type_str)
);
zend_string_release(type_str);
}
static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_ref_error(zend_property_info *prop) {
zend_string *type_str = zend_type_to_string(prop->type);
zend_type_error(
"Cannot auto-initialize an array inside a reference held by property %s::$%s of type %s",
ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
ZSTR_VAL(type_str)
);
zend_string_release(type_str);
}
static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_error(
zend_property_info *prop) {
zend_throw_error(NULL,
"Cannot access uninitialized non-nullable property %s::$%s by reference",
ZSTR_VAL(prop->ce->name),
zend_get_unmangled_property_name(prop->name));
}
/* this should modify object only if it's empty */
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_throw_non_object_error(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC)
{
zend_string *tmp_property_name;
zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
if (opline->opcode == ZEND_PRE_INC_OBJ
|| opline->opcode == ZEND_PRE_DEC_OBJ
|| opline->opcode == ZEND_POST_INC_OBJ
|| opline->opcode == ZEND_POST_DEC_OBJ) {
zend_throw_error(NULL,
"Attempt to increment/decrement property \"%s\" on %s",
ZSTR_VAL(property_name), zend_zval_type_name(object)
);
} else if (opline->opcode == ZEND_FETCH_OBJ_W
|| opline->opcode == ZEND_FETCH_OBJ_RW
|| opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
|| opline->opcode == ZEND_ASSIGN_OBJ_REF) {
zend_throw_error(NULL,
"Attempt to modify property \"%s\" on %s",
ZSTR_VAL(property_name), zend_zval_type_name(object)
);
} else {
zend_throw_error(NULL,
"Attempt to assign property \"%s\" on %s",
ZSTR_VAL(property_name), zend_zval_type_name(object)
);
}
zend_tmp_string_release(tmp_property_name);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_NULL(EX_VAR(opline->result.var));
}
}
static ZEND_COLD void zend_verify_type_error_common(
const zend_function *zf, const zend_arg_info *arg_info, zval *value,
const char **fname, const char **fsep, const char **fclass,
zend_string **need_msg, const char **given_kind)
{
*fname = ZSTR_VAL(zf->common.function_name);
if (zf->common.scope) {
*fsep = "::";
*fclass = ZSTR_VAL(zf->common.scope->name);
} else {
*fsep = "";
*fclass = "";
}
*need_msg = zend_type_to_string_resolved(arg_info->type, zf->common.scope);
if (value) {
*given_kind = zend_zval_type_name(value);
} else {
*given_kind = "none";
}
}
ZEND_API ZEND_COLD void zend_verify_arg_error(
const zend_function *zf, const zend_arg_info *arg_info, uint32_t arg_num, zval *value)
{
zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
const char *fname, *fsep, *fclass;
zend_string *need_msg;
const char *given_msg;
zend_verify_type_error_common(
zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
ZEND_ASSERT(zf->common.type == ZEND_USER_FUNCTION
&& "Arginfo verification is not performed for internal functions");
if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
zend_argument_type_error(arg_num, "must be of type %s, %s given, called in %s on line %d",
ZSTR_VAL(need_msg), given_msg,
ZSTR_VAL(ptr->func->op_array.filename), ptr->opline->lineno
);
} else {
zend_argument_type_error(arg_num,
"must be of type %s, %s given", ZSTR_VAL(need_msg), given_msg);
}
zend_string_release(need_msg);
}
static bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
{
zend_long lval;
double dval;
zend_string *str;
bool bval;
/* Type preference order: int -> float -> string -> bool */
if (type_mask & MAY_BE_LONG) {
/* For an int|float union type and string value,
* determine chosen type by is_numeric_string() semantics. */
if ((type_mask & MAY_BE_DOUBLE) && Z_TYPE_P(arg) == IS_STRING) {
zend_uchar type = is_numeric_str_function(Z_STR_P(arg), &lval, &dval);
if (type == IS_LONG) {
zend_string_release(Z_STR_P(arg));
ZVAL_LONG(arg, lval);
return 1;
}
if (type == IS_DOUBLE) {
zend_string_release(Z_STR_P(arg));
ZVAL_DOUBLE(arg, dval);
return 1;
}
} else if (zend_parse_arg_long_weak(arg, &lval, 0)) {
zval_ptr_dtor(arg);
ZVAL_LONG(arg, lval);
return 1;
} else if (UNEXPECTED(EG(exception))) {
return 0;
}
}
if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, 0)) {
zval_ptr_dtor(arg);
ZVAL_DOUBLE(arg, dval);
return 1;
}
if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, &str, 0)) {
/* on success "arg" is converted to IS_STRING */
return 1;
}
if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, 0)) {
zval_ptr_dtor(arg);
ZVAL_BOOL(arg, bval);
return 1;
}
return 0;
}
#if ZEND_DEBUG
static bool can_convert_to_string(zval *zv) {
/* We don't call cast_object here, because this check must be side-effect free. As this
* is only used for a sanity check of arginfo/zpp consistency, it's okay if we accept
* more than actually allowed here. */
if (Z_TYPE_P(zv) == IS_OBJECT) {
return Z_OBJ_HT_P(zv)->cast_object != zend_std_cast_object_tostring
|| Z_OBJCE_P(zv)->__tostring;
}
return Z_TYPE_P(zv) <= IS_STRING;
}
/* Used to sanity-check internal arginfo types without performing any actual type conversions. */
static bool zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask, zval *arg)
{
zend_long lval;
double dval;
bool bval;
/* Pass (uint32_t)-1 as arg_num to indicate to ZPP not to emit any deprecation notice,
* this is needed because the version with side effects also uses 0 (e.g. for typed properties) */
if ((type_mask & MAY_BE_LONG) && zend_parse_arg_long_weak(arg, &lval, (uint32_t)-1)) {
return 1;
}
if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, (uint32_t)-1)) {
return 1;
}
if ((type_mask & MAY_BE_STRING) && can_convert_to_string(arg)) {
return 1;
}
if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, (uint32_t)-1)) {
return 1;
}
return 0;
}
#endif
ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg)
{
if (UNEXPECTED(strict)) {
/* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
if (!(type_mask & MAY_BE_DOUBLE) || Z_TYPE_P(arg) != IS_LONG) {
return 0;
}
} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
/* NULL may be accepted only by nullable hints (this is already checked).
* As an exception for internal functions, null is allowed for scalar types in weak mode. */
return is_internal_arg
&& (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING));
}
#if ZEND_DEBUG
if (is_internal_arg) {
return zend_verify_weak_scalar_type_hint_no_sideeffect(type_mask, arg);
}
#endif
return zend_verify_weak_scalar_type_hint(type_mask, arg);
}
ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_info *info, zval *property)
{
zend_string *type_str;
/* we _may_ land here in case reading already errored and runtime cache thus has not been updated (i.e. it contains a valid but unrelated info) */
if (EG(exception)) {
return;
}
type_str = zend_type_to_string(info->type);
zend_type_error("Cannot assign %s to property %s::$%s of type %s",
zend_zval_type_name(property),
ZSTR_VAL(info->ce->name),
zend_get_unmangled_property_name(info->name),
ZSTR_VAL(type_str));
zend_string_release(type_str);
}
ZEND_COLD void zend_match_unhandled_error(zval *value)
{
smart_str msg = {0};
if (Z_TYPE_P(value) <= IS_STRING) {
smart_str_append_scalar(&msg, value, EG(exception_string_param_max_len));
} else {
smart_str_appendl(&msg, "of type ", sizeof("of type ")-1);
smart_str_appends(&msg, zend_zval_type_name(value));
}
smart_str_0(&msg);
zend_throw_exception_ex(
zend_ce_unhandled_match_error, 0, "Unhandled match case %s", ZSTR_VAL(msg.s));
smart_str_free(&msg);
}
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(
zend_property_info *info) {
zend_throw_error(NULL, "Cannot modify readonly property %s::$%s",
ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
}
static zend_class_entry *resolve_single_class_type(zend_string *name, zend_class_entry *self_ce) {
if (zend_string_equals_literal_ci(name, "self")) {
return self_ce;
} else if (zend_string_equals_literal_ci(name, "parent")) {
return self_ce->parent;
} else {
return zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
}
}
static zend_always_inline zend_class_entry *zend_ce_from_type(
zend_property_info *info, zend_type *type) {
ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*type));
zend_string *name = ZEND_TYPE_NAME(*type);
if (ZSTR_HAS_CE_CACHE(name)) {
zend_class_entry *ce = ZSTR_GET_CE_CACHE(name);
if (!ce) {
ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
}
return ce;
}
return resolve_single_class_type(name, info->ce);
}
static bool zend_check_and_resolve_property_class_type(
zend_property_info *info, zend_class_entry *object_ce) {
if (ZEND_TYPE_HAS_LIST(info->type)) {
zend_type *list_type;
if (ZEND_TYPE_IS_INTERSECTION(info->type)) {
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
zend_class_entry *ce = zend_ce_from_type(info, list_type);
if (!ce || !instanceof_function(object_ce, ce)) {
return false;
}
} ZEND_TYPE_LIST_FOREACH_END();
return true;
} else {
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
zend_class_entry *ce = zend_ce_from_type(info, list_type);
if (ce && instanceof_function(object_ce, ce)) {
return true;
}
} ZEND_TYPE_LIST_FOREACH_END();
return false;
}
} else {
zend_class_entry *ce = zend_ce_from_type(info, &info->type);
return ce && instanceof_function(object_ce, ce);
}
}
static zend_always_inline bool i_zend_check_property_type(zend_property_info *info, zval *property, bool strict)
{
ZEND_ASSERT(!Z_ISREF_P(property));
if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE_P(property)))) {
return 1;
}
if (ZEND_TYPE_IS_COMPLEX(info->type) && Z_TYPE_P(property) == IS_OBJECT
&& zend_check_and_resolve_property_class_type(info, Z_OBJCE_P(property))) {
return 1;
}
uint32_t type_mask = ZEND_TYPE_FULL_MASK(info->type);
ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
if ((type_mask & MAY_BE_ITERABLE) && zend_is_iterable(property)) {
return 1;
}
return zend_verify_scalar_type_hint(type_mask, property, strict, 0);
}
static zend_always_inline bool i_zend_verify_property_type(zend_property_info *info, zval *property, bool strict)
{
if (i_zend_check_property_type(info, property, strict)) {
return 1;
}
zend_verify_property_type_error(info, property);
return 0;
}
ZEND_API bool zend_never_inline zend_verify_property_type(zend_property_info *info, zval *property, bool strict) {
return i_zend_verify_property_type(info, property, strict);
}
static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *info, zval *property_val, zval *value EXECUTE_DATA_DC)
{
zval tmp;
if (UNEXPECTED(info->flags & ZEND_ACC_READONLY)) {
zend_readonly_property_modification_error(info);
return &EG(uninitialized_zval);
}
ZVAL_DEREF(value);
ZVAL_COPY(&tmp, value);
if (UNEXPECTED(!i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES()))) {
zval_ptr_dtor(&tmp);
return &EG(uninitialized_zval);
}
return zend_assign_to_variable(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES());
}
static zend_always_inline bool zend_value_instanceof_static(zval *zv) {
if (Z_TYPE_P(zv) != IS_OBJECT) {
return 0;
}
zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
if (!called_scope) {
return 0;
}
return instanceof_function(Z_OBJCE_P(zv), called_scope);
}
/* The cache_slot may only be NULL in debug builds, where arginfo verification of
* internal functions is enabled. Avoid unnecessary checks in release builds. */
#if ZEND_DEBUG
# define HAVE_CACHE_SLOT (cache_slot != NULL)
#else
# define HAVE_CACHE_SLOT 1
#endif
static zend_always_inline zend_class_entry *zend_fetch_ce_from_cache_slot(
void **cache_slot, zend_type *type)
{
if (EXPECTED(HAVE_CACHE_SLOT && *cache_slot)) {
return (zend_class_entry *) *cache_slot;
}
zend_string *name = ZEND_TYPE_NAME(*type);
zend_class_entry *ce;
if (ZSTR_HAS_CE_CACHE(name)) {
ce = ZSTR_GET_CE_CACHE(name);
if (!ce) {
ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
if (UNEXPECTED(!ce)) {
/* Cannot resolve */
return NULL;
}
}
} else {
ce = zend_fetch_class(name,
ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
if (UNEXPECTED(!ce)) {
return NULL;
}
}
if (HAVE_CACHE_SLOT) {
*cache_slot = (void *) ce;
}
return ce;