Skip to content

Commit 6ba10a0

Browse files
committed
Revert "Use ZEND_CLOSURE_OBJECT() macro to resolve closure op_array to closure object through address calculation, instead of op_array->prototype reuse." (this patch is incomplete or wrong)
This reverts commit 781e157.
1 parent da8204b commit 6ba10a0

8 files changed

+31
-28
lines changed

Zend/zend_closures.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ void zend_register_closure_ce(void) /* {{{ */
639639

640640
static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */
641641
{
642-
zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func));
642+
zend_closure *closure = (zend_closure*)EX(func)->common.prototype;
643643
closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
644644
OBJ_RELEASE((zend_object*)closure);
645645
EX(func) = NULL;
@@ -662,6 +662,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
662662

663663
if (func->type == ZEND_USER_FUNCTION) {
664664
memcpy(&closure->func, func, sizeof(zend_op_array));
665+
closure->func.common.prototype = (zend_function*)closure;
665666
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
666667
if (closure->func.op_array.static_variables) {
667668
closure->func.op_array.static_variables =
@@ -676,6 +677,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
676677
}
677678
} else {
678679
memcpy(&closure->func, func, sizeof(zend_internal_function));
680+
closure->func.common.prototype = (zend_function*)closure;
679681
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
680682
/* wrap internal function handler to avoid memory leak */
681683
if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {

Zend/zend_closures.h

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
BEGIN_EXTERN_C()
2626

27-
/* This macro depends on zend_closure structure layout */
28-
#define ZEND_CLOSURE_OBJECT(op_array) \
29-
((zend_object*)((char*)(op_array) - sizeof(zend_object)))
30-
3127
void zend_register_closure_ce(void);
3228
void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var);
3329

Zend/zend_execute.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,7 @@ static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t o
25542554
OBJ_RELEASE(Z_OBJ(call->This));
25552555
}
25562556
if (call->func->common.fn_flags & ZEND_ACC_CLOSURE) {
2557-
zend_object_release(ZEND_CLOSURE_OBJECT(call->func));
2557+
zend_object_release((zend_object *) call->func->common.prototype);
25582558
} else if (call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
25592559
zend_string_release(call->func->common.function_name);
25602560
zend_free_trampoline(call->func);
@@ -2735,7 +2735,8 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zval *
27352735

27362736
if (fbc->common.fn_flags & ZEND_ACC_CLOSURE) {
27372737
/* Delay closure destruction until its invocation */
2738-
GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc));
2738+
ZEND_ASSERT(GC_TYPE((zend_object*)fbc->common.prototype) == IS_OBJECT);
2739+
GC_ADDREF((zend_object*)fbc->common.prototype);
27392740
call_info |= ZEND_CALL_CLOSURE;
27402741
if (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
27412742
call_info |= ZEND_CALL_FAKE_CLOSURE;

Zend/zend_execute_API.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) /
776776
if (UNEXPECTED(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
777777
uint32_t call_info;
778778

779-
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
779+
ZEND_ASSERT(GC_TYPE((zend_object*)func->op_array.prototype) == IS_OBJECT);
780+
GC_ADDREF((zend_object*)func->op_array.prototype);
780781
call_info = ZEND_CALL_CLOSURE;
781782
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
782783
call_info |= ZEND_CALL_FAKE_CLOSURE;

Zend/zend_generators.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "zend_interfaces.h"
2525
#include "zend_exceptions.h"
2626
#include "zend_generators.h"
27-
#include "zend_closures.h"
2827

2928
ZEND_API zend_class_entry *zend_ce_generator;
3029
ZEND_API zend_class_entry *zend_ce_ClosedGeneratorException;
@@ -146,7 +145,7 @@ ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished
146145

147146
/* Free closure object */
148147
if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
149-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
148+
OBJ_RELEASE((zend_object *) EX(func)->common.prototype);
150149
}
151150

152151
/* Free GC buffer. GC for closed generators doesn't need an allocated buffer */
@@ -336,7 +335,7 @@ static HashTable *zend_generator_get_gc(zval *object, zval **table, int *n) /* {
336335
ZVAL_OBJ(gc_buffer++, Z_OBJ(execute_data->This));
337336
}
338337
if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
339-
ZVAL_OBJ(gc_buffer++, ZEND_CLOSURE_OBJECT(EX(func)));
338+
ZVAL_OBJ(gc_buffer++, (zend_object *) EX(func)->common.prototype);
340339
}
341340

342341
if (generator->node.children == 0) {

Zend/zend_vm_def.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ ZEND_VM_HELPER(zend_leave_helper, ANY, ANY)
23742374
}
23752375
OBJ_RELEASE(object);
23762376
} else if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
2377-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
2377+
OBJ_RELEASE((zend_object*)execute_data->func->op_array.prototype);
23782378
}
23792379
EG(vm_stack_top) = (zval*)execute_data;
23802380
execute_data = EX(prev_execute_data);
@@ -2405,7 +2405,7 @@ ZEND_VM_HELPER(zend_leave_helper, ANY, ANY)
24052405
}
24062406
OBJ_RELEASE(object);
24072407
} else if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
2408-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
2408+
OBJ_RELEASE((zend_object*)execute_data->func->op_array.prototype);
24092409
}
24102410

24112411
zend_vm_stack_free_extra_args_ex(call_info, execute_data);
@@ -2447,7 +2447,7 @@ ZEND_VM_HELPER(zend_leave_helper, ANY, ANY)
24472447
}
24482448
EG(current_execute_data) = EX(prev_execute_data);
24492449
if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
2450-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
2450+
OBJ_RELEASE((zend_object*)EX(func)->op_array.prototype);
24512451
}
24522452
ZEND_VM_RETURN();
24532453
} else /* if (call_kind == ZEND_CALL_TOP_CODE) */ {
@@ -3356,7 +3356,8 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
33563356
}
33573357
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
33583358
/* Delay closure destruction until its invocation */
3359-
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
3359+
ZEND_ASSERT(GC_TYPE((zend_object*)func->common.prototype) == IS_OBJECT);
3360+
GC_ADDREF((zend_object*)func->common.prototype);
33603361
call_info |= ZEND_CALL_CLOSURE;
33613362
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
33623363
call_info |= ZEND_CALL_FAKE_CLOSURE;
@@ -3369,7 +3370,7 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
33693370
FREE_OP2();
33703371
if ((OP2_TYPE & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
33713372
if (call_info & ZEND_CALL_CLOSURE) {
3372-
zend_object_release(ZEND_CLOSURE_OBJECT(func));
3373+
zend_object_release((zend_object*)func->common.prototype);
33733374
}
33743375
if (call_info & ZEND_CALL_RELEASE_THIS) {
33753376
zend_object_release(object);
@@ -4531,7 +4532,7 @@ ZEND_VM_HANDLER(119, ZEND_SEND_ARRAY, ANY, ANY, NUM)
45314532
}
45324533
zend_internal_type_error(EX_USES_STRICT_TYPES(), "call_user_func_array() expects parameter 2 to be array, %s given", zend_get_type_by_const(Z_TYPE_P(args)));
45334534
if (ZEND_CALL_INFO(EX(call)) & ZEND_CALL_CLOSURE) {
4534-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(call)->func));
4535+
OBJ_RELEASE((zend_object*)EX(call)->func->common.prototype);
45354536
}
45364537
if (Z_TYPE(EX(call)->This) == IS_OBJECT) {
45374538
OBJ_RELEASE(Z_OBJ(EX(call)->This));

Zend/zend_vm_execute.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_leave_helper_SPEC(ZEND_OPCODE_
457457
}
458458
OBJ_RELEASE(object);
459459
} else if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
460-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
460+
OBJ_RELEASE((zend_object*)execute_data->func->op_array.prototype);
461461
}
462462
EG(vm_stack_top) = (zval*)execute_data;
463463
execute_data = EX(prev_execute_data);
@@ -488,7 +488,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_leave_helper_SPEC(ZEND_OPCODE_
488488
}
489489
OBJ_RELEASE(object);
490490
} else if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
491-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
491+
OBJ_RELEASE((zend_object*)execute_data->func->op_array.prototype);
492492
}
493493

494494
zend_vm_stack_free_extra_args_ex(call_info, execute_data);
@@ -530,7 +530,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_leave_helper_SPEC(ZEND_OPCODE_
530530
}
531531
EG(current_execute_data) = EX(prev_execute_data);
532532
if (UNEXPECTED(call_info & ZEND_CALL_CLOSURE)) {
533-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
533+
OBJ_RELEASE((zend_object*)EX(func)->op_array.prototype);
534534
}
535535
ZEND_VM_RETURN();
536536
} else /* if (call_kind == ZEND_CALL_TOP_CODE) */ {
@@ -1361,7 +1361,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_SEND_ARRAY_SPEC_HANDLER(ZEND_O
13611361
}
13621362
zend_internal_type_error(EX_USES_STRICT_TYPES(), "call_user_func_array() expects parameter 2 to be array, %s given", zend_get_type_by_const(Z_TYPE_P(args)));
13631363
if (ZEND_CALL_INFO(EX(call)) & ZEND_CALL_CLOSURE) {
1364-
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(call)->func));
1364+
OBJ_RELEASE((zend_object*)EX(call)->func->common.prototype);
13651365
}
13661366
if (Z_TYPE(EX(call)->This) == IS_OBJECT) {
13671367
OBJ_RELEASE(Z_OBJ(EX(call)->This));
@@ -5585,7 +5585,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_CONS
55855585
}
55865586
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
55875587
/* Delay closure destruction until its invocation */
5588-
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
5588+
ZEND_ASSERT(GC_TYPE((zend_object*)func->common.prototype) == IS_OBJECT);
5589+
GC_ADDREF((zend_object*)func->common.prototype);
55895590
call_info |= ZEND_CALL_CLOSURE;
55905591
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
55915592
call_info |= ZEND_CALL_FAKE_CLOSURE;
@@ -5597,7 +5598,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_CONS
55975598

55985599
if ((IS_CONST & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
55995600
if (call_info & ZEND_CALL_CLOSURE) {
5600-
zend_object_release(ZEND_CLOSURE_OBJECT(func));
5601+
zend_object_release((zend_object*)func->common.prototype);
56015602
}
56025603
if (call_info & ZEND_CALL_RELEASE_THIS) {
56035604
zend_object_release(object);
@@ -7795,7 +7796,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_TMPV
77957796
}
77967797
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
77977798
/* Delay closure destruction until its invocation */
7798-
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
7799+
ZEND_ASSERT(GC_TYPE((zend_object*)func->common.prototype) == IS_OBJECT);
7800+
GC_ADDREF((zend_object*)func->common.prototype);
77997801
call_info |= ZEND_CALL_CLOSURE;
78007802
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
78017803
call_info |= ZEND_CALL_FAKE_CLOSURE;
@@ -7808,7 +7810,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_TMPV
78087810
zval_ptr_dtor_nogc(free_op2);
78097811
if (((IS_TMP_VAR|IS_VAR) & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
78107812
if (call_info & ZEND_CALL_CLOSURE) {
7811-
zend_object_release(ZEND_CLOSURE_OBJECT(func));
7813+
zend_object_release((zend_object*)func->common.prototype);
78127814
}
78137815
if (call_info & ZEND_CALL_RELEASE_THIS) {
78147816
zend_object_release(object);
@@ -10931,7 +10933,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_CV_H
1093110933
}
1093210934
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
1093310935
/* Delay closure destruction until its invocation */
10934-
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
10936+
ZEND_ASSERT(GC_TYPE((zend_object*)func->common.prototype) == IS_OBJECT);
10937+
GC_ADDREF((zend_object*)func->common.prototype);
1093510938
call_info |= ZEND_CALL_CLOSURE;
1093610939
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
1093710940
call_info |= ZEND_CALL_FAKE_CLOSURE;
@@ -10943,7 +10946,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_INIT_USER_CALL_SPEC_CONST_CV_H
1094310946

1094410947
if ((IS_CV & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
1094510948
if (call_info & ZEND_CALL_CLOSURE) {
10946-
zend_object_release(ZEND_CLOSURE_OBJECT(func));
10949+
zend_object_release((zend_object*)func->common.prototype);
1094710950
}
1094810951
if (call_info & ZEND_CALL_RELEASE_THIS) {
1094910952
zend_object_release(object);

ext/reflection/php_reflection.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ ZEND_METHOD(reflection_generator, getFunction)
22182218

22192219
if (ex->func->common.fn_flags & ZEND_ACC_CLOSURE) {
22202220
zval closure;
2221-
ZVAL_OBJ(&closure, ZEND_CLOSURE_OBJECT(ex->func));
2221+
ZVAL_OBJ(&closure, (zend_object *) ex->func->common.prototype);
22222222
reflection_function_factory(ex->func, &closure, return_value);
22232223
} else if (ex->func->op_array.scope) {
22242224
reflection_method_factory(ex->func->op_array.scope, ex->func, NULL, return_value);

0 commit comments

Comments
 (0)