Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,14 @@ static zend_always_inline void zend_call_known_instance_method_with_1_params(
ZEND_API void zend_call_known_instance_method_with_2_params(
zend_function *fn, zend_object *this_ptr, zval *retval_ptr, zval *param1, zval *param2);

/* Call a resolved user/internal function with two by-value arguments.
* Excludes deprecated/generator functions, trampolines, by-reference passing for either parameter. */
ZEND_API zend_result zend_call_known_user_function_with_2_params(
const zend_fcall_info_cache *fcc, zval *retval, zval *arg1, zval *arg2);

ZEND_API zend_result zend_call_known_internal_function_with_2_params(
const zend_fcall_info_cache *fcc, zval *retval, zval *arg1, zval *arg2);

/* Call method if it exists.
* Return FAILURE if method does not exist.
* If FAILURE is returned, retval will be UNDEF, and error will contain the validation failure message if provided.
Expand Down
139 changes: 139 additions & 0 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,145 @@ ZEND_API void zend_call_known_instance_method_with_2_params(
zend_call_known_instance_method(fn, this_ptr, retval_ptr, 2, params);
}

static zend_always_inline zend_result zend_call_known_function_with_2_params(
const zend_fcall_info_cache *fcc, zval *retval_ptr, zval *arg1, zval *arg2, bool internal)
{
zval unused_retval;
zval *retval = retval_ptr ? retval_ptr : &unused_retval;

ZVAL_UNDEF(retval);
if (!EG(active)) {
return FAILURE;
}
if (ZEND_HAS_OOM()) {
zend_handle_delayed_oom();
ZEND_ASSERT(EG(exception));
}
if (EG(exception)) {
return SUCCESS;
}
#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_call_stack_size_error();
return SUCCESS;
}
#endif

zend_function *func = fcc->function_handler;
ZEND_ASSERT(func && func->type == (internal ? ZEND_INTERNAL_FUNCTION : ZEND_USER_FUNCTION));
ZEND_ASSERT(!(func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_DEPRECATED
| ZEND_ACC_GENERATOR)));
ZEND_ASSERT(!ARG_SHOULD_BE_SENT_BY_REF(func, 1) && !ARG_SHOULD_BE_SENT_BY_REF(func, 2));
zend_object *object = (func->common.fn_flags & ZEND_ACC_STATIC) ? NULL : fcc->object;
uint32_t call_info = ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC;
if (object) {
GC_ADDREF(object);
call_info |= ZEND_CALL_HAS_THIS;
}
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
call_info |= ZEND_CALL_CLOSURE;
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
call_info |= ZEND_CALL_FAKE_CLOSURE;
}
}
zend_execute_data *call = zend_vm_stack_push_call_frame(call_info, func, 2,
object ? (void *) object : (void *) fcc->called_scope);
ZVAL_COPY_DEREF(ZEND_CALL_ARG(call, 1), arg1);
ZVAL_COPY_DEREF(ZEND_CALL_ARG(call, 2), arg2);

const zend_class_entry *orig_fake_scope = EG(fake_scope);
EG(fake_scope) = NULL;
if (!internal) {
uint32_t orig_jit_trace_num = EG(jit_trace_num);
zend_init_func_execute_data(call, &func->op_array, retval);
ZEND_OBSERVER_FCALL_BEGIN(call);
zend_execute_ex(call);
EG(jit_trace_num) = orig_jit_trace_num;
} else {
ZVAL_NULL(retval);
call->prev_execute_data = EG(current_execute_data);
EG(current_execute_data) = call;
#if ZEND_DEBUG
bool should_throw = zend_internal_call_should_throw(func, call);
#endif
ZEND_OBSERVER_FCALL_BEGIN(call);
if (EXPECTED(zend_execute_internal == NULL)) {
func->internal_function.handler(call, retval);
} else {
zend_execute_internal(call, retval);
}
#if ZEND_DEBUG
if (!EG(exception) && !ZEND_HAS_OOM() && call->func) {
if (should_throw) {
zend_internal_call_arginfo_violation(call->func);
}
if (call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
bool result = zend_verify_internal_return_type(call->func, retval);
ZEND_ASSERT(result);
}
ZEND_ASSERT(((call->func->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) && !zend_return_should_unwrap_ref(call))
? Z_ISREF_P(retval) : !Z_ISREF_P(retval));
ZEND_ASSERT(!(call->func->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS));
}
#endif
ZEND_OBSERVER_FCALL_END(call, retval);
EG(current_execute_data) = call->prev_execute_data;
zend_vm_stack_free_args(call);
if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)) {
zend_array_release(call->extra_named_params);
}
if (EG(exception)) {
zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
}

if (UNEXPECTED(atomic_exchange(&EG(vm_interrupt), false))) {
if (atomic_load(&EG(timed_out))) {
zend_timeout();
} else if (ZEND_HAS_OOM()) {
zend_handle_delayed_oom();
zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
} else if (zend_interrupt_function) {
zend_interrupt_function(EG(current_execute_data));
}
}
if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS)) {
OBJ_RELEASE(Z_OBJ(call->This));
}
EG(fake_scope) = orig_fake_scope;
if (object) {
OBJ_RELEASE(object);
}
zend_vm_stack_free_call_frame(call);
if (UNEXPECTED(EG(exception))) {
if (UNEXPECTED(!EG(current_execute_data))) {
zend_throw_exception_internal(NULL);
} else if (EG(current_execute_data)->func
&& ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
zend_rethrow_exception(EG(current_execute_data));
}
}
if (!retval_ptr) {
zval_ptr_dtor(&unused_retval);
}
return SUCCESS;
}

ZEND_API zend_result zend_call_known_user_function_with_2_params(
const zend_fcall_info_cache *fcc, zval *retval, zval *arg1, zval *arg2)
{
return zend_call_known_function_with_2_params(fcc, retval, arg1, arg2, false);
}

ZEND_API zend_result zend_call_known_internal_function_with_2_params(
const zend_fcall_info_cache *fcc, zval *retval, zval *arg1, zval *arg2)
{
return zend_call_known_function_with_2_params(fcc, retval, arg1, arg2, true);
}

ZEND_API zend_result zend_call_method_if_exists_ex(
zend_object *this_ptr, zend_string *method_name, zval *retval_ptr,
uint32_t param_count, zval *params, HashTable *named_params, char **error
Expand Down
63 changes: 58 additions & 5 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,35 @@ PHP_FUNCTION(ksort)
}
/* }}} */

static zend_always_inline zend_function *php_array_get_known_compare_func(const zend_fcall_info_cache *fcc)
{
zend_function *func = fcc->function_handler;
if (func && (func->type == ZEND_USER_FUNCTION || func->type == ZEND_INTERNAL_FUNCTION)
&& !(func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_DEPRECATED
| ZEND_ACC_GENERATOR))
&& !ARG_SHOULD_BE_SENT_BY_REF(func, 1) && !ARG_SHOULD_BE_SENT_BY_REF(func, 2)) {
return func;
}
return NULL;
}

/* A comparator can replace or clear the shared callback state while comparisons are running, for
* example through a nested array_udiff_assoc() call, a failed callable check or a fiber switch.
* Use the specialized call only while the cache still holds the previously checked function. */
static zend_always_inline void php_array_call_user_compare(zval *args, zval *retval)
{
const zend_function *func = BG(user_compare_fci_cache).function_handler;
if (EXPECTED(func != NULL && func == BG(user_compare_known_func))) {

@drealecs drealecs Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eligibility decision is cached, but the shared callback state can still change. There is backup/restore handling for ordinary nested calls, however, array_udiff_assoc() / array_uintersect_assoc() currently overwrite that state without restoring it, and also fiber switches can resume another operation before restoration happens.

#24453 and #24426 address those underlying issues. Once both are merged, and the fiber save/restore also includes user_compare_known_func, we can remove this identity check.
Generic fallback would still be needed for ineligible callbacks.

if (EXPECTED(func->type == ZEND_USER_FUNCTION)) {
zend_call_known_user_function_with_2_params(&BG(user_compare_fci_cache), retval, &args[0], &args[1]);
} else {
zend_call_known_internal_function_with_2_params(&BG(user_compare_fci_cache), retval, &args[0], &args[1]);
}
} else {
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
}
}

static inline int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
{
zval args[2];
Expand All @@ -766,7 +795,7 @@ static inline int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ *
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval = &retval;
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
php_array_call_user_compare(args, &retval);

if (UNEXPECTED(Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
if (!ARRAYG(compare_deprecation_thrown)) {
Expand All @@ -780,7 +809,7 @@ static inline int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ *
/* Retry with swapped operands. */
ZVAL_COPY_VALUE(&args[0], &s->val);
ZVAL_COPY_VALUE(&args[1], &f->val);
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
php_array_call_user_compare(args, &retval);

zend_long ret = php_get_long(&retval);
return -ZEND_NORMALIZE_BOOL(ret);
Expand All @@ -800,17 +829,21 @@ static int php_array_user_compare(Bucket *a, Bucket *b) /* {{{ */

#define PHP_ARRAY_CMP_FUNC_VARS \
zend_fcall_info old_user_compare_fci; \
zend_fcall_info_cache old_user_compare_fci_cache \
zend_fcall_info_cache old_user_compare_fci_cache; \
zend_function *old_user_compare_known_func \

#define PHP_ARRAY_CMP_FUNC_BACKUP() \
old_user_compare_fci = BG(user_compare_fci); \
old_user_compare_fci_cache = BG(user_compare_fci_cache); \
old_user_compare_known_func = BG(user_compare_known_func); \
ARRAYG(compare_deprecation_thrown) = 0; \
BG(user_compare_fci_cache) = empty_fcall_info_cache; \
BG(user_compare_known_func) = NULL; \

#define PHP_ARRAY_CMP_FUNC_RESTORE() \
BG(user_compare_fci) = old_user_compare_fci; \
BG(user_compare_fci_cache) = old_user_compare_fci_cache; \
BG(user_compare_known_func) = old_user_compare_known_func; \

static void php_usort(INTERNAL_FUNCTION_PARAMETERS, bucket_compare_func_t compare_func, bool renumber) /* {{{ */
{
Expand All @@ -831,6 +864,8 @@ static void php_usort(INTERNAL_FUNCTION_PARAMETERS, bucket_compare_func_t compar
RETURN_TRUE;
}

BG(user_compare_known_func) = php_array_get_known_compare_func(&BG(user_compare_fci_cache));

/* Copy array, so the in-place modifications will not be visible to the callback function */
arr = zend_array_dup(arr);

Expand Down Expand Up @@ -879,7 +914,7 @@ static inline int php_array_user_key_compare_unstable(Bucket *f, Bucket *s) /* {
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval = &retval;
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
php_array_call_user_compare(args, &retval);

if (UNEXPECTED(Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
if (!ARRAYG(compare_deprecation_thrown)) {
Expand All @@ -902,7 +937,7 @@ static inline int php_array_user_key_compare_unstable(Bucket *f, Bucket *s) /* {
ZVAL_STR(&args[1], f->key);
}

zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
php_array_call_user_compare(args, &retval);

zend_long ret = php_get_long(&retval);
return -ZEND_NORMALIZE_BOOL(ret);
Expand Down Expand Up @@ -5332,6 +5367,10 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
}

PHP_ARRAY_CMP_FUNC_BACKUP();
zend_function *known_data_func = data_compare_type == INTERSECT_COMP_DATA_USER
? php_array_get_known_compare_func(fci_data_cache) : NULL;
zend_function *known_key_func = key_compare_type == INTERSECT_COMP_KEY_USER
? php_array_get_known_compare_func(fci_key_cache) : NULL;

/* for each argument, create and sort list with pointers to the hash buckets */
lists = (Bucket **)safe_emalloc(arr_argc, sizeof(Bucket *), 0);
Expand All @@ -5340,9 +5379,11 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
if (behavior == INTERSECT_NORMAL && data_compare_type == INTERSECT_COMP_DATA_USER) {
BG(user_compare_fci) = *fci_data;
BG(user_compare_fci_cache) = *fci_data_cache;
BG(user_compare_known_func) = known_data_func;
} else if ((behavior & INTERSECT_ASSOC) && key_compare_type == INTERSECT_COMP_KEY_USER) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
}

for (i = 0; i < arr_argc; i++) {
Expand Down Expand Up @@ -5399,6 +5440,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
&& key_compare_type == INTERSECT_COMP_KEY_USER) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
}

for (i = 1; i < arr_argc; i++) {
Expand All @@ -5418,12 +5460,14 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
if (data_compare_type == INTERSECT_COMP_DATA_USER) {
BG(user_compare_fci) = *fci_data;
BG(user_compare_fci_cache) = *fci_data_cache;
BG(user_compare_known_func) = known_data_func;
}
if (intersect_data_compare_func(ptrs[0], ptrs[i]) != 0) {
c = 1;
if (key_compare_type == INTERSECT_COMP_KEY_USER) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
/* When KEY_USER, the last parameter is always the callback */
}
/* we are going to the break */
Expand Down Expand Up @@ -5936,6 +5980,10 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
}

PHP_ARRAY_CMP_FUNC_BACKUP();
zend_function *known_data_func = data_compare_type == DIFF_COMP_DATA_USER
? php_array_get_known_compare_func(fci_data_cache) : NULL;
zend_function *known_key_func = key_compare_type == DIFF_COMP_KEY_USER
? php_array_get_known_compare_func(fci_key_cache) : NULL;

/* for each argument, create and sort list with pointers to the hash buckets */
lists = (Bucket **)safe_emalloc(arr_argc, sizeof(Bucket *), 0);
Expand All @@ -5944,9 +5992,11 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
if (behavior == DIFF_NORMAL && data_compare_type == DIFF_COMP_DATA_USER) {
BG(user_compare_fci) = *fci_data;
BG(user_compare_fci_cache) = *fci_data_cache;
BG(user_compare_known_func) = known_data_func;
} else if ((behavior & DIFF_ASSOC) && key_compare_type == DIFF_COMP_KEY_USER) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
}

for (i = 0; i < arr_argc; i++) {
Expand Down Expand Up @@ -6000,6 +6050,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
}
c = 1;
for (i = 1; i < arr_argc; i++) {
Expand All @@ -6026,13 +6077,15 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
if (data_compare_type == DIFF_COMP_DATA_USER) {
BG(user_compare_fci) = *fci_data;
BG(user_compare_fci_cache) = *fci_data_cache;
BG(user_compare_known_func) = known_data_func;
}
if (diff_data_compare_func(ptrs[0], ptr) != 0) {
/* the data is not the same */
c = -1;
if (key_compare_type == DIFF_COMP_KEY_USER) {
BG(user_compare_fci) = *fci_key;
BG(user_compare_fci_cache) = *fci_key_cache;
BG(user_compare_known_func) = known_key_func;
}
} else {
break;
Expand Down
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */
BG(locale_changed) = 0;
BG(user_compare_fci) = empty_fcall_info;
BG(user_compare_fci_cache) = empty_fcall_info_cache;
BG(user_compare_known_func) = NULL;
BG(page_uid) = -1;
BG(page_gid) = -1;
BG(page_inode) = -1;
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ typedef struct _php_basic_globals {
size_t strtok_len;
zend_fcall_info user_compare_fci;
zend_fcall_info_cache user_compare_fci_cache;
/* Eligible function identity for specialized PHP/native comparison calls, or NULL. */
zend_function *user_compare_known_func;
zend_llist *user_tick_functions;

zval active_ini_file_section;
Expand Down
Loading
Loading