forked from runkit7/runkit7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunkit_functions.c
1696 lines (1526 loc) · 65.6 KB
/
runkit_functions.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group, (c) 2008-2015 Dmitry Zenovich |
| "runkit7" patches (c) 2015-2020 Tyson Andre |
+----------------------------------------------------------------------+
| This source file is subject to the new BSD 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.opensource.org/licenses/BSD-3-Clause |
| or at https://github.com/runkit7/runkit7/blob/master/LICENSE |
+----------------------------------------------------------------------+
| Author: Sara Golemon <[email protected]> |
| Modified by Dmitry Zenovich <[email protected]> |
| Modified for php7 by Tyson Andre <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "runkit.h"
#include "php_runkit_hash.h"
#include "php_runkit_zend_execute_API.h"
#include "Zend/zend.h"
#if PHP_VERSION_ID >= 80000
#include "Zend/zend_attributes.h"
#endif
#ifdef ZEND_MAP_PTR_GET
#define RUNKIT_RUN_TIME_CACHE(op_array) \
ZEND_MAP_PTR_GET((op_array)->run_time_cache)
#else
#define RUNKIT_RUN_TIME_CACHE(op_array) \
((op_array)->run_time_cache)
#endif
/* {{{ php_runkit7_obtain_internal_function_rid() */
static int php_runkit7_obtain_internal_function_rid() {
// Obtain a reserved slot index that isn't used by any other internal extensions such as performance monitors.
int rid = RUNKIT_G(original_func_resource_handle);
ZEND_ASSERT(rid >= 0);
return rid;
} /* }}} */
// Get lvalue of the aliased user function for a fake internal function.
#define RUNKIT_ALIASED_USER_FUNCTION(fe) ((fe)->internal_function.reserved[php_runkit7_obtain_internal_function_rid()])
#define RUNKIT_IS_ALIAS_FOR_USER_FUNCTION(fe) ((fe)->type == ZEND_INTERNAL_FUNCTION && (fe)->internal_function.handler == php_runkit_function_alias_handler)
extern ZEND_API void zend_vm_set_opcode_handler(zend_op *op);
#ifdef PHP_RUNKIT_MANIPULATION
/* {{{ Top level declarations */
static void php_runkit_function_copy_ctor_same_type(zend_function *fe, zend_string *newname);
/* }}} */
/* {{{ php_runkit_check_call_stack
*/
int php_runkit_check_call_stack(zend_op_array *op_array)
{
zend_execute_data *ptr;
ptr = EG(current_execute_data);
while (ptr) {
// TODO: ???
if (ptr->func && ptr->func->op_array.opcodes == op_array->opcodes) {
return FAILURE;
}
ptr = ptr->prev_execute_data;
}
return SUCCESS;
}
/* }}} */
/* Temporary function name. This function is manipulated with functions in this file. */
#define RUNKIT_TEMP_FUNCNAME "__runkit_temporary_function__"
/* Temporary class name. A function in this class is manipulated with functions in this file. */
#define RUNKIT_TEMP_CLASSNAME "__runkit_temporary_class__"
/* Temporary method name. A function in this class is manipulated with functions in this file. */
#define RUNKIT_TEMP_METHODNAME "__runkit_temporary_method__"
/* Maintain order */
#define PHP_RUNKIT_FETCH_FUNCTION_INSPECT 0
#define PHP_RUNKIT_FETCH_FUNCTION_REMOVE 1
#define PHP_RUNKIT_FETCH_FUNCTION_RENAME 2
/* {{{ php_runkit_fetch_function
*/
static zend_function *php_runkit_fetch_function(zend_string *fname, int flag)
{
zend_function *fe;
zend_string *fname_lower;
fname_lower = zend_string_tolower(fname);
if ((fe = zend_hash_find_ptr(EG(function_table), fname_lower)) == NULL) {
zend_string_release(fname_lower);
php_error_docref(NULL, E_WARNING, "%s() not found", ZSTR_VAL(fname));
return NULL;
}
if (fe->type == ZEND_INTERNAL_FUNCTION &&
!RUNKIT_G(internal_override)) {
zend_string_release(fname_lower);
php_error_docref(NULL, E_WARNING, "%s() is an internal function and runkit.internal_override is disabled", ZSTR_VAL(fname));
return NULL;
}
if (fe->type != ZEND_USER_FUNCTION &&
fe->type != ZEND_INTERNAL_FUNCTION) {
zend_string_release(fname_lower);
php_error_docref(NULL, E_WARNING, "%s() is not a user or normal internal function", ZSTR_VAL(fname));
return NULL;
}
if (fe->type == ZEND_INTERNAL_FUNCTION &&
flag >= PHP_RUNKIT_FETCH_FUNCTION_REMOVE) {
if (!RUNKIT_G(replaced_internal_functions)) {
ALLOC_HASHTABLE(RUNKIT_G(replaced_internal_functions));
zend_hash_init(RUNKIT_G(replaced_internal_functions), 4, NULL, NULL, 0);
}
// FIXME figure out what this is intended to do (Restoring internal functions on request shutdown)
// This is also used to check if a function with a given name was originally internal.
// Cloning this - It would otherwise be deleted with runkit_function_redefine called later... (TODO: Add 1 to the refcount?)
// TODO: Properly specify the behavior to avoid memory leaks
if (!zend_hash_exists(RUNKIT_G(replaced_internal_functions), fname_lower)) {
Bucket *b;
zend_function *fe_copy;
// Copy over the original function - If the original function remains, it will be freed on module shutdown.
zend_string_addref(fe->common.function_name); // possibly unnecessary
fe_copy = php_runkit_function_clone(fe, fe->common.function_name, ZEND_INTERNAL_FUNCTION);
// Copy over the original persistent string - That string wouldn't be destroyed on request shutdown in fpm, and can be reused in future requests?
b = php_runkit_zend_hash_find_bucket(EG(function_table), fname_lower);
// php_error_docref(NULL, E_WARNING, "Finding persistent string %s: %llx\n", ZSTR_VAL(fname_lower), (long long)(uintptr_t)b);
// It's a persistent string, not an interned string? Is it always a persistent string (E.g. in NTS, ZTS, maintainer ZTS)?
if (b->key != NULL) {
zend_string_addref(b->key);
zend_string_release(fname_lower);
fname_lower = b->key;
// php_error_docref(NULL, E_WARNING, "Stealing persistent string %s\n", ZSTR_VAL(fname_lower));
} else {
zend_string_addref(fname_lower);
}
zend_hash_add_ptr(RUNKIT_G(replaced_internal_functions), fname_lower, fe_copy);
}
// printf("Adding fe %llx to replaced_internal_functions key=%s result=%llx\n", (long long)fe, ZSTR_VAL(fname_lower), (long long)result);
/*
* If internal functions have been modified then runkit's request shutdown handler
* should be called after all other modules' ones.
*/
php_runkit_hash_move_runkit_to_front();
EG(full_tables_cleanup) = 1; // dirty hack!
}
zend_string_release(fname_lower);
return fe;
}
/* }}} */
/* {{{ php_runkit_ensure_misplaced_internal_functions_table_exists */
static inline void php_runkit_ensure_misplaced_internal_functions_table_exists()
{
if (!RUNKIT_G(misplaced_internal_functions)) {
ALLOC_HASHTABLE(RUNKIT_G(misplaced_internal_functions));
zend_hash_init(RUNKIT_G(misplaced_internal_functions), 4, NULL, NULL, 0);
}
}
/* }}} */
/* {{{ php_runkit_add_to_misplaced_internal_functions */
static inline void php_runkit_add_to_misplaced_internal_functions(zend_function *fe, zend_string *name_lower)
{
if (fe->type == ZEND_INTERNAL_FUNCTION &&
(!RUNKIT_G(misplaced_internal_functions) ||
!zend_hash_exists(RUNKIT_G(misplaced_internal_functions), name_lower))
) {
zval tmp;
php_runkit_ensure_misplaced_internal_functions_table_exists();
// Add misplaced internal functions to a list of strings, to be wiped out on request shutdown (before restoring originals)
ZVAL_STR(&tmp, name_lower);
zend_string_addref(name_lower); // TODO: Free this in calls to php_runkit_destroy_misplaced_internal_function on shutdown?
zend_hash_next_index_insert(RUNKIT_G(misplaced_internal_functions), &tmp);
}
}
/* }}} */
/* {{{ php_runkit_destroy_misplaced_internal_function */
static inline void php_runkit_destroy_misplaced_internal_function(zend_function *fe, zend_string *fname_lower)
{
if (fe->type == ZEND_INTERNAL_FUNCTION && RUNKIT_G(misplaced_internal_functions) &&
zend_hash_exists(RUNKIT_G(misplaced_internal_functions), fname_lower)) {
// PHP_RUNKIT_FREE_INTERNAL_FUNCTION_NAME would have been called, but function destructor already deletes those?
zend_hash_del(RUNKIT_G(misplaced_internal_functions), fname_lower);
}
}
/* }}} */
#ifdef RT_CONSTANT_EX
/* {{{ php_runkit_set_opcode_constant
for absolute constant addresses (ZEND_USE_ABS_CONST_ADDR), changes op to point to the local copy of that literal.
Modifies op's contents. */
static void php_runkit_set_opcode_constant(const zval *literals, znode_op *op, zval *literalI)
{
debug_printf("php_runkit_set_opcode_constant(%llx, %llx, %d), USE_ABS=%d\n", (long long)literals, (long long)literalI, (int)sizeof(zval), ZEND_USE_ABS_CONST_ADDR);
#if ZEND_USE_ABS_CONST_ADDR
RT_CONSTANT_EX(literals, *op) = literalI;
#else
// TODO: Assert that this is in a meaningful range.
// TODO: is this ever necessary for relative constant addresses?
op->constant = ((char *)literalI) - ((char *)literals);
#endif
}
/* }}} */
#else
/* {{{ php_runkit_set_opcode_constant_relative
for absolute constant addresses, creates a local copy of that literal.
Modifies op's contents. */
static void php_runkit_set_opcode_constant_relative(const zend_op_array *op_array, const zend_op *opline, znode_op *op, zval *literalI)
{
debug_printf("php_runkit_set_opcode_constant_relative(%llx, %d), USE_ABS=%d\n", (long long)literalI, (int)sizeof(zval), ZEND_USE_ABS_CONST_ADDR);
#if ZEND_USE_ABS_CONST_ADDR
RUNKIT_RT_CONSTANT(op_array, opline, *op) = literalI;
#else
debug_printf("opcodes=%llx literals=%llx opline=%llx op=%llx literalI=%llx\n", (long long)op_array->opcodes, (long long)op_array->literals, (long long)opline, (long long)op, (long long)literalI);
// TODO: Assert that this is in a meaningful range.
// TODO: is this ever necessary for relative constant addresses?
// Opposite of ((zval*)(((char*)(opline)) + (int32_t)(node).constant))
op->constant = ((char *)literalI) - ((char *)opline);
#endif
}
/* }}} */
#endif
/* {{{ php_runkit_function_alias_handler
Used when an internal function is replaced by a user-defined/runkit function. Converts the ICALL to a UCALL.
Params: zend_execute_data *execute_data, zval *return_value */
static ZEND_NAMED_FUNCTION(php_runkit_function_alias_handler)
{
zend_function *fbc_inner;
zend_function *fbc = execute_data->func;
int result;
#if ZEND_DEBUG
ZEND_ASSERT(fbc->type == ZEND_INTERNAL_FUNCTION);
#endif
fbc_inner = (zend_function *)RUNKIT_ALIASED_USER_FUNCTION(fbc);
// fprintf(stderr, "Fetched handler %llx from fbc=%llx aliased=%p reserved=%p\n", (long long)(uintptr_t)fbc_inner, (long long)(uintptr_t)fbc, RUNKIT_ALIASED_USER_FUNCTION(fbc), fbc->internal_function.reserved);
#if ZEND_DEBUG
ZEND_ASSERT(fbc_inner->type == ZEND_USER_FUNCTION);
#endif
// printf("In php_runkit_function_alias_handler! fbc=%llx fbc_inner=%llx execute_data=%llx return_value=%llx\n", (long long) fbc, (long long) fbc_inner, (long long)execute_data, (long long)return_value);
// TODO: Pass a context?
// TODO: Copy the implementation of zend_call_function, use it to set up an additional stack entry....
// FIXME modify current_execute_data for stack traces.
// FIXME : 4th param check_this should be 1 if this is a method.
// Fake the stack and call the inner function.
// Set up the execution of the new command.
result = runkit_forward_call_user_function(fbc, fbc_inner, INTERNAL_FUNCTION_PARAM_PASSTHRU);
(void)result;
// printf("In php_runkit_function_alias_handler! execute_data=%llx return_value=%llx, return code = %d\n", (long long)execute_data, (long long)return_value, (int)result);
// TODO: Throw an exception
return;
// FIXME: This is wrong, it did not start executing the command, it only set it up for execution. Try doing something similar to call_user_func_array?
}
/* }}} */
/* {{{ php_runkit_function_create_alias_internal_function */
/** Create a fake internal function which will call the duplicated user function instead. */
static void php_runkit_function_create_alias_internal_function(zend_function *fe, zend_function *fe_inner)
{
// Zero out the parts that will be an internal function.
memset((((uint8_t *)fe) + sizeof(fe->common)), 0, sizeof(zend_function) - sizeof(fe->common));
#if ZEND_DEBUG
ZEND_ASSERT(fe_inner->type == ZEND_USER_FUNCTION);
#endif
// TODO: Figure out refcount tracking of function names when aliases are used.
fe->type = ZEND_INTERNAL_FUNCTION;
fe->common.function_name = fe_inner->common.function_name;
zend_string_addref(fe->common.function_name);
debug_printf("Copying handler to %llx\n", (long long)(uintptr_t)php_runkit_function_alias_handler);
fe->internal_function.handler = php_runkit_function_alias_handler;
RUNKIT_ALIASED_USER_FUNCTION(fe) = fe_inner;
// fprintf(stderr, "Copying handler to %llx for fbc=%llx aliased=%p reserved=%p\n", (long long)(uintptr_t)php_runkit_function_alias_handler, (long long)(uintptr_t)fe, RUNKIT_ALIASED_USER_FUNCTION(fe), fe->internal_function.reserved);
}
/* }}} */
/* {{{ php_runkit_function_copy_ctor
Duplicate structures in a zend_function where necessary to make an outright duplicate.
Does additional work to ensure that the type of the final function is orig_fe_type. */
int php_runkit_function_copy_ctor(zend_function *fe, zend_string *newname, char orig_fe_type)
{
if (fe->type == orig_fe_type) {
php_runkit_function_copy_ctor_same_type(fe, newname);
return SUCCESS;
} else if (orig_fe_type == ZEND_INTERNAL_FUNCTION) { /* We replaced an internal function with a user function. */
zend_function *fe_inner = pemalloc(sizeof(zend_op_array), 1);
memcpy(fe_inner, fe, sizeof(zend_op_array));
php_runkit_function_copy_ctor_same_type(fe_inner, newname);
php_runkit_function_create_alias_internal_function(fe, fe_inner);
// printf("Allocated fe_inner=%llx type=%d\n", (long long)fe_inner, (int)fe_inner->type);
return SUCCESS;
}
php_runkit_function_copy_ctor_same_type(fe, newname);
return SUCCESS;
}
/* }}} */
/* {{{ runkit_allocate_opcode_copy
Allocates a zend_op_array with enough memory to store the opcodes (As well as literals, if required for php 7.3) */
static zend_op *runkit_allocate_opcode_copy(const zend_op_array *const op_array)
{
#if PHP_VERSION_ID >= 70300
// Adjustment for https://github.com/runkit7/runkit7/issues/126
// I assume that PHP's pass_two() from Zend/zend_opcache.c would be done by now
if (!(ZEND_USE_ABS_CONST_ADDR) &&
(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) &&
op_array->literals) {
// Allocate enough space for op_array->opcode_copy and op_array->last_literal()
size_t bytes_to_allocate = ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
sizeof(zval) * op_array->last_literal;
zend_op *opcode = (zend_op *)emalloc(bytes_to_allocate);
debug_printf("Allocating %d bytes for opcode array last=%d last_literal=%d\n", (int)bytes_to_allocate, (int)op_array->last, (int)op_array->last_literal);
memset(opcode, 0, bytes_to_allocate);
return opcode;
// Skip the memcpy step for the op_array and the literals in php 7.3+
}
#endif
debug_printf("Allocating %d bytes for opcode array without literals\n", (int)(sizeof(zend_op) * op_array->last));
return safe_emalloc(sizeof(zend_op), op_array->last, 0);
}
/* }}} */
/* {{{ runkit_allocate_literals */
static zval *runkit_allocate_literals(const zend_op_array *const op_array, zend_op *opcode_copy)
{
#if PHP_VERSION_ID >= 70300
// Adjustment for https://github.com/runkit7/runkit7/issues/126
// I assume that PHP's pass_two() from Zend/zend_opcode.c would be done by now
if (!(ZEND_USE_ABS_CONST_ADDR) &&
(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) {
// Reuse the extra space allocated for runkit_allocate_opcode_copy
debug_printf("Allocating literals at relative offset %d last=%d\n", (int)(ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16)), (int)op_array->last);
return (zval *)(((char *)opcode_copy) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
}
#else
(void)opcode_copy;
#endif
debug_printf("Allocating literals with emalloc last_literal=%d\n", op_array->last_literal);
return safe_emalloc(op_array->last_literal, sizeof(zval), 0);
}
/* }}} */
/* {{{ php_runkit_arginfo_type_addref
Adds a reference to the type or union type of this argument. See zend_type_release() and destroy_op_array() from Zend/zend_opcode.c */
static void php_runkit_arginfo_type_addref(zend_arg_info *arginfo)
{
#if PHP_VERSION_ID < 80000
if (ZEND_TYPE_IS_CLASS(arginfo->type)) {
// This works as the opposite for both php 7.2 and 7.3 (zend_string_release or zend_string_release_ex
zend_string_addref(ZEND_TYPE_NAME(arginfo->type));
}
#else
zend_type type = arginfo->type;
if (ZEND_TYPE_HAS_LIST(type)) {
zend_type *atomic_type;
zend_type_list *type_list = ZEND_TYPE_LIST(type);
size_t size_in_bytes = ZEND_TYPE_LIST_SIZE(type_list->num_types);
zend_type_list *new_type_list = emalloc(size_in_bytes);
memcpy(new_type_list, type_list, size_in_bytes);
arginfo->type.ptr = new_type_list;
ZEND_TYPE_LIST_FOREACH(type_list, atomic_type) {
if (ZEND_TYPE_HAS_NAME(*atomic_type)) {
zend_string_addref(ZEND_TYPE_NAME(*atomic_type));
}
} ZEND_TYPE_LIST_FOREACH_END();
} else if (ZEND_TYPE_HAS_NAME(type)) {
zend_string_addref(ZEND_TYPE_NAME(type));
}
#endif
}
/* }}} */
/* {{{ php_runkit_function_copy_ctor_same_type
Duplicates structures in an zend_function, creating a function of the same type (user/internal) as the original function
This does the opposite of some parts of destroy_op_array() from Zend/zend_opcode.c
*/
static void php_runkit_function_copy_ctor_same_type(zend_function *fe, zend_string *newname)
{
zval *literals;
void *run_time_cache;
zend_string **dupvars; // Array of zend_string*s
zend_op *last_op;
zend_op *opcode_copy;
zend_op_array *const op_array = &(fe->op_array);
uint32_t i;
if (newname) {
zend_string_addref(newname);
fe->common.function_name = newname;
} else {
zend_string_addref(fe->common.function_name);
// TODO: is this a memory leak?
// fe->common.function_name = fe->common.function_name;
}
if (fe->common.type == ZEND_USER_FUNCTION) {
if (op_array->vars) {
i = op_array->last_var;
dupvars = safe_emalloc(op_array->last_var, sizeof(zend_string *), 0);
while (i > 0) {
i--;
dupvars[i] = op_array->vars[i];
zend_string_addref(dupvars[i]);
}
op_array->vars = dupvars;
}
// TODO: Remove ZEND_ACC_IMMUTABLE from func.common.fn_flags, like php 7.4?
// TODO: Look into other php 7.4 changes required for d57cd36e47b627dee5b825760163f8e62e23ab28
if (op_array->static_variables) {
// Similar to zend_closures.c's zend_create_closure copying static variables, zend_compile.c's do_bind_function
// TODO: Does that work with references?
// 979: This seems to be calling an internal function returning a reference, then crashing?
// ZEND_ASSERT((call->func->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)
// ? Z_ISREF_P(ret) : !Z_ISREF_P(ret));
op_array->static_variables = zend_array_dup(op_array->static_variables);
#if PHP_VERSION_ID >= 70400
ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &(op_array->static_variables));
#endif
}
if (RUNKIT_RUN_TIME_CACHE(op_array)) {
// TODO: Garbage collect these, somehow?
run_time_cache = pemalloc(op_array->cache_size, 1);
memset(run_time_cache, 0, op_array->cache_size);
#ifdef ZEND_MAP_PTR_SET
ZEND_MAP_PTR_SET(op_array->run_time_cache, run_time_cache);
#else
op_array->run_time_cache = run_time_cache;
#endif
}
opcode_copy = runkit_allocate_opcode_copy(op_array);
last_op = op_array->opcodes + op_array->last;
// TODO: See if this code works on 32-bit PHP.
for (i = 0; i < op_array->last; i++) {
opcode_copy[i] = op_array->opcodes[i];
debug_printf("opcode = %s, is_const=%d, constant=%d\n", zend_get_opcode_name((int)opcode_copy[i].opcode), (int)(opcode_copy[i].op1_type == IS_CONST), op_array->opcodes[i].op1.constant);
if (opcode_copy[i].op1_type != IS_CONST) {
// TODO: What about switch statements in php 7.2, etc?
zend_op *opline;
zend_op *jmp_addr_op1;
switch (opcode_copy[i].opcode) {
#ifdef ZEND_GOTO
case ZEND_GOTO:
#endif
#ifdef ZEND_FAST_CALL
case ZEND_FAST_CALL:
#endif
case ZEND_JMP:
opline = &opcode_copy[i];
jmp_addr_op1 = OP_JMP_ADDR(opline, opline->op1);
// TODO: I don't know if this is really the same as jmp_addr. FIXME
if (jmp_addr_op1 >= op_array->opcodes &&
jmp_addr_op1 < last_op) {
#if ZEND_USE_ABS_JMP_ADDR
opline->op1.jmp_addr = opcode_copy + (op_array->opcodes[i].op1.jmp_addr - op_array->opcodes);
#else
opline->op1.jmp_offset += ((char *)op_array->opcodes) - ((char *)opcode_copy);
#endif
}
}
}
if (opcode_copy[i].op2_type != IS_CONST) {
zend_op *opline;
zend_op *jmp_addr_op2;
switch (opcode_copy[i].opcode) {
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
#ifdef ZEND_JMP_SET
case ZEND_JMP_SET:
#endif
#ifdef ZEND_JMP_SET_VAR
case ZEND_JMP_SET_VAR:
#endif
opline = &opcode_copy[i];
jmp_addr_op2 = OP_JMP_ADDR(opline, opline->op2);
// TODO: I don't know if this is really the same as jmp_addr. FIXME
if (jmp_addr_op2 >= op_array->opcodes &&
jmp_addr_op2 < last_op) {
#if ZEND_USE_ABS_JMP_ADDR
opline->op2.jmp_addr = opcode_copy + (op_array->opcodes[i].op2.jmp_addr - op_array->opcodes);
#else
opline->op2.jmp_offset += ((char *)op_array->opcodes) - ((char *)opcode_copy);
#endif
}
}
}
}
debug_printf("op_array.literals = %llx, last_literal = %d\n", (long long)op_array->literals, op_array->last_literal);
if (op_array->literals) {
uint32_t k;
debug_printf(" old op_array.literals = %llx, opcodes=%llx\n", (long long)op_array->literals, (long long)op_array->opcodes);
literals = runkit_allocate_literals(op_array, opcode_copy);
debug_printf("copied op_array.literals = %llx, opcodes=%llx\n", (long long)literals, (long long)opcode_copy);
for (i = op_array->last_literal; i > 0; ) {
i--;
// This code copies the zvals from the original function's op array to the new function's op array.
// TODO: Re-examine this line to see if reference counting was done properly.
// TODO: fix all of the other places old types of copies were used.
literals[i] = op_array->literals[i];
Z_TRY_ADDREF(literals[i]);
debug_printf("Copying literal of type %d\n", (int)Z_TYPE(literals[i]));
}
for (k = 0; k < op_array->last; k++) {
zend_op *const new_op = &opcode_copy[k];
zend_bool found_op1 = 0;
zend_bool found_op2 = 0;
debug_printf("%d(%s)\ttype=%d %d\n", k, zend_get_opcode_name(new_op->opcode), (int)new_op->op1_type, (int)new_op->op2_type);
for (i = op_array->last_literal; i > 0; ) {
i--;
// TODO: This may be completely unnecessary on 64-bit systems. This may be broken on 32-bit systems.
#ifdef RT_CONSTANT_EX
if (!found_op1 && new_op->op1_type == IS_CONST && RT_CONSTANT_EX(literals, new_op->op1) == &op_array->literals[i]) {
debug_printf("old op1 constant #%d = %d\n", (int)k, new_op->op1.constant);
php_runkit_set_opcode_constant(literals, &(new_op->op1), &literals[i]);
debug_printf("new op1 constant #%d = %d\n", (int)k, new_op->op1.constant);
found_op1 = 1;
}
if (!found_op2 && new_op->op2_type == IS_CONST && RT_CONSTANT_EX(literals, new_op->op2) == &op_array->literals[i]) {
debug_printf("old op2 constant #%d = %d\n", (int)k, new_op->op2.constant);
php_runkit_set_opcode_constant(literals, &(new_op->op2), &literals[i]);
debug_printf("new op2 constant #%d = %d\n", (int)k, new_op->op2.constant);
found_op2 = 1;
}
#else
// e.g. this is used on 64-bit builds of PHP.
// The start of the literals zval array is memory aligned so the relative addressing may be different when copied.
if (!found_op1 && new_op->op1_type == IS_CONST && RUNKIT_RT_CONSTANT(op_array, &op_array->opcodes[k], new_op->op1) == &op_array->literals[i]) {
debug_printf("old op1 constant #%d = %d (v2)\n", (int)k, new_op->op1.constant);
php_runkit_set_opcode_constant_relative(op_array, new_op, &(new_op->op1), &literals[i]);
debug_printf("new op1 constant #%d = %d (v2)\n", (int)k, new_op->op1.constant);
found_op1 = 1;
}
if (!found_op2 && new_op->op2_type == IS_CONST && RUNKIT_RT_CONSTANT(op_array, &op_array->opcodes[k], new_op->op2) == &op_array->literals[i]) {
debug_printf("old op2 constant #%d = %d (v2)\n", (int)k, new_op->op2.constant);
php_runkit_set_opcode_constant_relative(op_array, new_op, &(new_op->op2), &literals[i]);
debug_printf("new op2 constant #%d = %d (v2)\n", (int)k, new_op->op2.constant);
found_op2 = 1;
}
#endif
}
}
op_array->literals = literals;
}
op_array->opcodes = opcode_copy;
op_array->refcount = (uint32_t *)emalloc(sizeof(uint32_t));
*op_array->refcount = 1;
if (op_array->doc_comment) {
zend_string_addref(op_array->doc_comment);
}
if (op_array->filename) {
zend_string_addref(op_array->filename);
}
op_array->try_catch_array = (zend_try_catch_element *)estrndup((char *)op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
if (op_array->live_range) {
op_array->live_range = (zend_live_range *)estrndup((char *)op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
}
/* TODO: Is the copying of arg info correct when copying internal arginfo to/from user-defined arg info? The offset for internal functions is always -1. */
if (op_array->arg_info) {
zend_arg_info *tmpArginfo;
zend_arg_info *originalArginfo;
// NOTE: This is the offset calculation for a user-defined function.
// num_args calculation taken from zend_opcode.c destroy_op_array
//
// TODO: Add tests that functions with return types are properly created and destroyed.
// TODO: Specify what runkit should do about return types, what is an error, what is valid.
uint32_t num_args = op_array->num_args;
int32_t offset = 0;
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
offset++;
num_args++;
}
if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
tmpArginfo = (zend_arg_info *)safe_emalloc(sizeof(zend_arg_info), num_args, 0);
originalArginfo = &((op_array->arg_info)[-offset]);
for (i = 0; i < num_args; i++) {
tmpArginfo[i] = originalArginfo[i];
if (tmpArginfo[i].name) {
zend_string_addref((tmpArginfo[i].name));
}
php_runkit_arginfo_type_addref(&tmpArginfo[i]);
}
op_array->arg_info = &tmpArginfo[offset];
}
} else {
/* This is an internal class */
#if PHP_VERSION_ID >= 80000
if ((op_array->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
op_array->arg_info) {
zend_arg_info *tmpArginfo;
zend_arg_info *originalArginfo;
// num_args calculation taken from zend_opcode.c free_internal_arg_info
// TODO: Add tests that functions with return types are properly created and destroyed.
// TODO: Specify what runkit should do about return types, what is an error, what is valid.
uint32_t num_args = op_array->num_args + 1;
const int32_t offset = 1;
if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
tmpArginfo = pemalloc(sizeof(zend_arg_info) * num_args, 1);
originalArginfo = &((op_array->arg_info)[-offset]);
for (i = 0; i < num_args; i++) {
tmpArginfo[i] = originalArginfo[i];
/*
if (tmpArginfo[i].name) {
zend_string_addref((tmpArginfo[i].name));
}
*/
php_runkit_arginfo_type_addref(&tmpArginfo[i]);
}
op_array->arg_info = &tmpArginfo[offset];
} else {
op_array->arg_info = NULL;
}
#endif
}
// I'm not sure why this was originally added in upstream -- Removing this isn't causing test failures in php 7.
// NOTE: If this line is uncommented, then PHP 7.3 would fail during cleanup of op arrays,
// because Zend's function destructor expects functions that didn't complete pass two to have a separate allocated array for literals.
// fe->common.fn_flags &= ~ZEND_ACC_DONE_PASS_TWO;
fe->common.prototype = fe;
}
/* }}} */
/* {{{ php_runkit_function_clone
Makes a duplicate of fe that doesn't share any static variables, zvals, etc.
TODO: Is there anything I can use from zend_duplicate_function? */
zend_function *php_runkit_function_clone(zend_function *fe, zend_string *newname, char orig_fe_type)
{
// Make a persistent allocation.
// TODO: Clean it up after a request?
zend_function *new_function = pemalloc(sizeof(zend_function), 1);
if (fe->type == ZEND_INTERNAL_FUNCTION) {
memset(new_function, 0, sizeof(zend_function));
memcpy(new_function, fe, sizeof(zend_internal_function));
} else {
memcpy(new_function, fe, sizeof(zend_function));
}
php_runkit_function_copy_ctor(new_function, newname, orig_fe_type);
return new_function;
}
/* }}} */
void php_runkit_free_inner_if_aliased_function(zend_function *fe)
{
if (RUNKIT_IS_ALIAS_FOR_USER_FUNCTION(fe)) {
zval zv_inner;
zend_function *fe_inner;
fe_inner = (zend_function *)RUNKIT_ALIASED_USER_FUNCTION(fe);
// TODO: Free these earlier if possible.
debug_printf("Freeing internal function %llx of %llx: %s\n", (long long)(uintptr_t)(fe_inner), (long long)(uintptr_t)fe, (char *)ZSTR_VAL(fe_inner->common.function_name));
#if ZEND_DEBUG
ZEND_ASSERT(fe_inner->type == ZEND_USER_FUNCTION);
#endif
ZVAL_FUNC(&zv_inner, fe_inner);
zend_function_dtor(&zv_inner);
pefree(fe_inner, 1);
}
}
/* {{{ php_runkit_function_dtor_impl - Destroys functions, handles special fields added if they're from runkit. */
void php_runkit_function_dtor_impl(zend_function *fe, zend_bool is_clone)
{
zval zv;
zend_bool is_user_function;
is_user_function = fe->type == ZEND_USER_FUNCTION;
php_runkit_free_inner_if_aliased_function(fe);
ZVAL_FUNC(&zv, fe);
zend_function_dtor(&zv);
// Note: This can only be used with zend_functions created by php_runkit_function_clone.
// ZEND_INTERNAL_FUNCTIONs are freed.
if (is_clone && is_user_function) {
pefree(fe, 1);
}
}
/* }}} */
/* {{{ php_runkit_function_dtor - Only to be used if we are sure this was created by runkit with runkit_function_clone. */
void php_runkit_function_dtor(zend_function *fe)
{
php_runkit_function_dtor_impl(fe, 1);
}
/* }}} */
// The original destructor of the affected function_table.
static dtor_func_t __function_table_orig_pDestructor = NULL;
static void php_runkit_function_table_dtor(zval *pDest)
{
zend_function *fe = (zend_function *)Z_PTR_P(pDest);
if (RUNKIT_IS_ALIAS_FOR_USER_FUNCTION(fe)) {
php_runkit_free_inner_if_aliased_function(fe);
} else {
// Don't free the inner if it's one of the ZEND_INTERNAL_FUNCTIONs moved elsewhere
if (fe->type != ZEND_INTERNAL_FUNCTION && __function_table_orig_pDestructor != NULL) {
__function_table_orig_pDestructor(pDest);
}
}
}
/* {{{ php_runkit_function_create_clone_alias_user_function - Create a user function aliasing an internal function */
static zend_function *php_runkit_function_create_clone_alias_user_function(zend_function *sfe)
{
#if ZEND_DEBUG
ZEND_ASSERT(sfe->type == ZEND_INTERNAL_FUNCTION);
#endif
/*
// FIXME support making user functions that are aliases of internal functions and callable.
// Currently, we just leave them as the original internal function.
if (1) {
zend_function *fe;
fe = emalloc(sizeof(zend_op_array), 1);
memcpy(fe, sfe, sizeof(sfe->common)); // Copy the common parts of internal function to the user function
memset((((uint8_t*) fe) + sizeof(fe->common)), 0, sizeof(zend_op) - sizeof(fe->common));
fe->op_array.refcount = (uint32_t*)emalloc(sizeof(uint32_t));
*(fe->op_array.refcount) = 1;
// TODO: initialize op_array.opcodes with the desired operations.
fe->reserved[0] = sfe;
return fe;
}
*/
return sfe;
}
/* }}} */
/* {{{ php_runkit_remove_from_function_table - This handles special cases where we associate aliased functions with the original */
int php_runkit_remove_from_function_table(HashTable *function_table, zend_string *func_lower)
{
// To be called for internal functions (TODO: internal methods?)
// TODO: Have a way to detect function clones.
int result;
__function_table_orig_pDestructor = function_table->pDestructor;
function_table->pDestructor = php_runkit_function_table_dtor;
result = zend_hash_del(function_table, func_lower);
function_table->pDestructor = __function_table_orig_pDestructor;
__function_table_orig_pDestructor = NULL;
return result;
}
/* }}} */
/* {{{ php_runkit_update_ptr_in_function_table - This handles special cases where we associate aliased functions with the original, and then replace that with zend_hash_update */
void *php_runkit_update_ptr_in_function_table(HashTable *function_table, zend_string *func_lower, zend_function *f)
{
// To be called for internal functions (TODO: internal methods?)
// TODO: Have a way to detect function clones.
void *result;
__function_table_orig_pDestructor = function_table->pDestructor;
function_table->pDestructor = php_runkit_function_table_dtor;
result = zend_hash_update_ptr(function_table, func_lower, f);
function_table->pDestructor = __function_table_orig_pDestructor;
__function_table_orig_pDestructor = NULL;
return result;
}
/* }}} */
/* {{{ runkit_zend_hash_add_or_update_function_table_ptr */
static inline void *runkit_zend_hash_add_or_update_function_table_ptr(HashTable *function_table, zend_string *key, void *pData, uint32_t flag)
{
void *result;
__function_table_orig_pDestructor = function_table->pDestructor;
function_table->pDestructor = php_runkit_function_table_dtor;
result = runkit_zend_hash_add_or_update_ptr(function_table, key, pData, flag);
function_table->pDestructor = __function_table_orig_pDestructor;
__function_table_orig_pDestructor = NULL;
return result;
}
/* }}} */
/* {{{ php_runkit_clear_function_runtime_cache */
static void php_runkit_clear_function_runtime_cache(zend_function *f)
{
zend_op_array *op_array;
if (f->type != ZEND_USER_FUNCTION) {
return;
}
op_array = &(f->op_array);
if (op_array->cache_size == 0 || RUNKIT_RUN_TIME_CACHE(op_array) == NULL) {
return;
}
// TODO: Does memset do what I want it to do?
memset(RUNKIT_RUN_TIME_CACHE(op_array), 0, op_array->cache_size);
}
/* }}} */
/* {{{ php_runkit_clear_function_runtime_cache_for_function_table */
static void php_runkit_clear_function_runtime_cache_for_function_table(HashTable *function_table) {
zend_function* f;
ZEND_HASH_FOREACH_PTR(function_table, f) {
php_runkit_clear_function_runtime_cache(f);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
/* {{{ php_runkit_clear_all_functions_runtime_cache */
void php_runkit_clear_all_functions_runtime_cache()
{
uint32_t i;
zend_execute_data *ptr;
zend_class_entry *ce;
php_runkit_clear_function_runtime_cache_for_function_table(EG(function_table));
ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
php_runkit_clear_function_runtime_cache_for_function_table(&(ce->function_table));
} ZEND_HASH_FOREACH_END();
// TODO: Does this make sense, does it work with a runtime cache?
for (ptr = EG(current_execute_data); ptr != NULL; ptr = ptr->prev_execute_data) {
// TODO: I assume that ptr->run_time_cache is the same pointer, if set?
if (ptr->func == NULL || ptr->func->type == ZEND_INTERNAL_FUNCTION || ptr->func->op_array.cache_size == 0 || RUNKIT_RUN_TIME_CACHE(&(ptr->func->op_array)) == NULL) {
continue;
}
memset(RUNKIT_RUN_TIME_CACHE(&(ptr->func->op_array)), 0, ptr->func->op_array.cache_size);
}
PHP_RUNKIT_ITERATE_THROUGH_OBJECTS_STORE_BEGIN(i)
if (object->ce == zend_ce_closure) {
zend_closure *cl = (zend_closure *)object;
php_runkit_clear_function_runtime_cache(&cl->func);
}
PHP_RUNKIT_ITERATE_THROUGH_OBJECTS_STORE_END
}
/* }}} */
/* {{{ php_runkit_fix_hardcoded_stack_sizes */
static inline void php_runkit_fix_hardcoded_stack_sizes(zend_function *f, zend_string *called_name_lower, zend_function *called_f)
{
// called_name_lower is the lowercase function name.
// f is a function which may or may not call called_name_lower, and may or may not have already been fixed.
// TODO: Make this work with namespaced function names
// TODO: Do method names also need to be fixed?
// TODO: Do I only need to worry about user function calls within the same file?
// FIXME work on namespaced functions
zend_op_array *op_array;
zend_op *opline_it;
zend_op *end;
if (f == NULL || f->type != ZEND_USER_FUNCTION) {
return;
}
op_array = &(f->op_array);
opline_it = op_array->opcodes;
end = opline_it + op_array->last;
//printf("Calling php_runkit_fix_hardcoded_stack_sizes for user func last=%d\n", op_array->last);
for (; opline_it < end; opline_it++) {
// printf("Calling opcode=%d\n", opline_it->opcode);
// PHP uses different constants (More constant space needed for ZEND_INIT_FCALL_BY_NAME),
// instead of converting to ZEND_INIT_FCALL_BY_NAME, recalculate the maximum amount of stack space opline_it may need.
if (opline_it->opcode == ZEND_INIT_FCALL) {
zval *function_name = (zval *)(RUNKIT_RT_CONSTANT(op_array, opline_it, opline_it->op2));
//printf("Checking init_fcall, function name = %s\n", ZSTR_VAL(Z_STR_P(function_name)));
if (zend_string_equals(Z_STR_P(function_name), called_name_lower)) {
// Modify that opline with the recalculated required stack size
uint32_t new_size = zend_vm_calc_used_stack(opline_it->extended_value, called_f);
if (new_size > opline_it->op1.num) {
opline_it->op1.num = new_size;
}
}
}
}
}
/* }}} */
/* {{{ php_runkit_fix_hardcoded_stack_sizes_for_function_table */
static void php_runkit_fix_hardcoded_stack_sizes_for_function_table(HashTable *function_table, zend_string *called_name_lower, zend_function *called_f)
{
zend_function *f;
ZEND_HASH_FOREACH_PTR(function_table, f) {
php_runkit_fix_hardcoded_stack_sizes(f, called_name_lower, called_f);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
/* {{{ php_runkit_fix_hardcoded_stack_sizes */
void php_runkit_fix_all_hardcoded_stack_sizes(zend_string *called_name_lower, zend_function *called_f)
{
uint32_t i;
zend_class_entry *ce;
zend_execute_data *ptr;
php_runkit_fix_hardcoded_stack_sizes_for_function_table(EG(function_table), called_name_lower, called_f);
ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
php_runkit_fix_hardcoded_stack_sizes_for_function_table(&(ce->function_table), called_name_lower, called_f);
} ZEND_HASH_FOREACH_END();
// This is also needed to get the top-level {main} script, which isn't in the function table
// FIXME what about other scripts that are include()ed
for (ptr = EG(current_execute_data); ptr != NULL; ptr = ptr->prev_execute_data) {
// TODO: I assume that ptr->run_time_cache is the same pointer, if set?
if (ptr->func == NULL || ptr->func->type != ZEND_USER_FUNCTION) {
continue;
}
php_runkit_fix_hardcoded_stack_sizes(ptr->func, called_name_lower, called_f);
}
PHP_RUNKIT_ITERATE_THROUGH_OBJECTS_STORE_BEGIN(i)
if (object->ce == zend_ce_closure) {
zend_closure *cl = (zend_closure *)object;
php_runkit_fix_hardcoded_stack_sizes(&cl->func, called_name_lower, called_f);
}
PHP_RUNKIT_ITERATE_THROUGH_OBJECTS_STORE_END
}
/* }}} */
/* {{{ php_runkit_reflection_update_property */
static void php_runkit_reflection_update_property(zend_object *object, const char *name, zval *value)
{
// Copied from ext/reflection's reflection_update_property
#if PHP_VERSION_ID >= 80000
zend_string *name_string = zend_string_init(name, strlen(name), 0);
zend_std_write_property(object, name_string, value, NULL);
zend_string_release(name_string);
#else
zval obj;
zval member;
ZVAL_OBJ(&obj, object);
ZVAL_STRING(&member, name);
zend_std_write_property(&obj, &member, value, NULL);
zval_ptr_dtor(&member);
#endif
if (Z_REFCOUNTED_P(value)) {
Z_DELREF_P(value);
}
}
/* }}} */
/* {{{ php_runkit_update_reflection_object_name */
void php_runkit_update_reflection_object_name(zend_object *object, int handle, const char *name)
{
zval prop_value;
ZVAL_STRING(&prop_value, name);
php_runkit_reflection_update_property(object, RUNKIT_G(name_str), &prop_value);
}
/* }}} */
/* {{{ php_runkit_free_reflection_function */
static void php_runkit_free_reflection_function(zend_function *fptr)
{
// Exact copy of ext/reflection's _free_function
if (fptr && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
zend_string_release(fptr->internal_function.function_name);
zend_free_trampoline(fptr);
}
}
/* }}} */
/* {{{ reflection_object_from_obj
Copied from ext/reflection/php_reflection.c */
static inline reflection_object *reflection_object_from_obj(zend_object *obj)
{
return (reflection_object *)((char *)(obj)-XtOffsetOf(reflection_object, zo));
}
/* }}} */
/* {{{ php_runkit_delete_reflection_function_ptr
Frees the parts of a reflection object referring to the removed method/function(/parameter?) */
static void php_runkit_delete_reflection_function_ptr(reflection_object *intern)
{
// Copied from ext/reflection's reflection_free_objects_storage
parameter_reference *reference;
if (intern->ptr) {
switch (intern->ref_type) {
case REF_TYPE_PARAMETER:
reference = (parameter_reference *)intern->ptr;
php_runkit_free_reflection_function(reference->fptr);
efree(intern->ptr);