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
5 changes: 2 additions & 3 deletions Zend/tests/functions/zend_call_function_deprecated_frame.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var_dump($a);
--EXPECTF--
Fatal error: Uncaught Exception: Function foo() is deprecated in %s:%d
Stack trace:
#0 [internal function]: {closure:%s:%d}(16384, 'Function foo() ...', '%s', %d)
#1 %s(%d): array_map(Object(Closure), Array)
#2 {main}
#0 %s(%d): {closure:%s:%d}(16384, 'Function foo() ...', '%s', %d)
#1 {main}
thrown in %s on line %d
5 changes: 2 additions & 3 deletions Zend/tests/gh14003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ array_filter(
--EXPECTF--
Fatal error: Uncaught Exception: Test in %s:%d
Stack trace:
#0 [internal function]: foo('a')
#1 %s(%d): array_map(Object(Closure), Array)
#2 {main}
#0 %s(%d): foo('a')
#1 {main}
thrown in %s on line %d
100 changes: 96 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5027,7 +5027,97 @@ static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *a
return SUCCESS;
}

static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
{
/* Bail out if we do not have exactly two parameters. */
if (args->children != 2) {
return FAILURE;
}

zend_ast *callback = args->child[0];

/* Bail out if the callback is not a FCC/PFA. */
if (callback->kind != ZEND_AST_CALL) {
return FAILURE;
}
/* Bail out if the callback is assert() due to the AST stringification logic
* breaking for the generated call.
*/
if (zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) {
return FAILURE;
}
zend_ast *args_ast = zend_ast_call_get_args(callback);
if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
return FAILURE;
}

znode value;
value.op_type = IS_TMP_VAR;
value.u.op.var = get_temporary_variable();

zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
zend_ast *call_args = zend_ast_create_list(0, ZEND_AST_ARG_LIST);
for (uint32_t i = 0; i < callback_args->children; i++) {
zend_ast *child = callback_args->child[i];
if (child->kind == ZEND_AST_PLACEHOLDER_ARG) {
call_args = zend_ast_list_add(call_args, zend_ast_create_znode(&value));
} else {
ZEND_ASSERT(0 && "not implemented");
call_args = zend_ast_list_add(call_args, child);
}
}
Comment on lines +5058 to +5068
Copy link
Member

Choose a reason for hiding this comment

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

For PFAs, each argument can be either of:

  • kind == ZEND_AST_PLACEHOLDER_ARG && attr == ZEND_PLACEHOLDER_VARIADIC: This is a ...
  • Else if kind == ZEND_AST_PLACEHOLDER_ARG: This is a ?
  • Else: This is a pre-bound arg (a normal arg)

I think that you could reuse zend_partial_apply() from https://github.com/php/php-src/pull/20848/changes#diff-85701127596aca0e597bd7961b5d59cdde4f6bb3e2a109a22be859ab7568b4d2R6704 to build call_args from callback_args.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, my plan was to either adjust this PR if PFA lands first or to adjust the PFA PR if that one lands first, since it is currently unreachable except for ....

I hope to follow up with array_filter() and array_find(), which might require multiple arguments to fill in. Perhaps the zend_partial_apply() could be made variadic to accept any number of values that are then filled into the correct positions if more than one placeholder is specified?


zend_op *opline;

znode array;
zend_compile_expr(&array, args->child[1]);

/* Verify that the input array actually is an array. */
znode name;
name.op_type = IS_CONST;
ZVAL_STR_COPY(&name.u.constant, lcname);
opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
opline->lineno = lineno;
opline->extended_value = (2 << 16) | IS_ARRAY;
const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;

/* Initialize the result array. */
zend_emit_op(result, ZEND_INIT_ARRAY, NULL, NULL);

/* foreach loop starts here. */
znode key;

uint32_t opnum_reset = get_next_op_number();
znode reset_node;
zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
uint32_t opnum_fetch = get_next_op_number();
zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);

/* loop body */
znode call_result;
zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args));
opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
SET_NODE(opline->result, result);
/* end loop body */

zend_emit_jump(opnum_fetch);

uint32_t opnum_loop_end = get_next_op_number();
opline = &CG(active_op_array)->opcodes[opnum_reset];
opline->op2.opline_num = opnum_loop_end;
opline = &CG(active_op_array)->opcodes[opnum_fetch];
opline->extended_value = opnum_loop_end;

zend_end_loop(opnum_fetch, &reset_node);
zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

result needs to be freed as well when it isn't used


return SUCCESS;
}

static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
{
if (zend_string_equals_literal(lcname, "strlen")) {
return zend_compile_func_strlen(result, args);
Expand Down Expand Up @@ -5099,12 +5189,14 @@ static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *
return zend_compile_func_printf(result, args);
} else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
return zend_compile_func_clone(result, args);
} else if (zend_string_equals_literal(lcname, "array_map")) {
return zend_compile_func_array_map(result, args, lcname, lineno);
} else {
return FAILURE;
}
}

static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */
{
if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
return FAILURE;
Expand All @@ -5120,7 +5212,7 @@ static zend_result zend_try_compile_special_func(znode *result, zend_string *lcn
return FAILURE;
}

if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
return SUCCESS;
}

Expand Down Expand Up @@ -5263,7 +5355,7 @@ static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type)

if (!is_callable_convert &&
zend_try_compile_special_func(result, lcname,
zend_ast_get_list(args_ast), fbc, type) == SUCCESS
zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
) {
zend_string_release_ex(lcname, 0);
zval_ptr_dtor(&name_node.u.constant);
Expand Down
32 changes: 32 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8858,6 +8858,38 @@ ZEND_VM_C_LABEL(type_check_resource):
}
}

ZEND_VM_HOT_HANDLER(211, ZEND_TYPE_ASSERT, CONST, ANY, NUM)
{
USE_OPLINE
SAVE_OPLINE();

zval *value = GET_OP2_ZVAL_PTR_UNDEF(BP_VAR_R);

uint8_t actual_type = Z_TYPE_P(value);
uint8_t expected_type = opline->extended_value & 0xff;
/* Simple types can be checked directly. */
if (UNEXPECTED(actual_type != expected_type)) {
zend_function *fbc;
{
zval *fname = (zval*)RT_CONSTANT(opline, opline->op1);
ZEND_ASSERT(Z_EXTRA_P(fname) != 0);
fbc = Z_FUNC(EG(function_table)->arData[Z_EXTRA_P(fname)].val);
ZEND_ASSERT(fbc->type != ZEND_USER_FUNCTION);
}
uint16_t argno = opline->extended_value >> 16;
zend_arg_info *arginfo = &fbc->common.arg_info[argno - 1];

if (!zend_check_type(&arginfo->type, value, /* scope */ NULL, /* is_return_type */ false, /* is_internal */ true)) {
const char *param_name = get_function_arg_name(fbc, argno);
zend_string *expected = zend_type_to_string(arginfo->type);
zend_type_error("%s(): Argument #%d%s%s%s must be of type %s, %s given", ZSTR_VAL(fbc->common.function_name), argno, param_name ? " ($" : "", param_name ? param_name : "", param_name ? ")" : "", ZSTR_VAL(expected), zend_zval_value_name(value));
zend_string_release(expected);
}
}

ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

ZEND_VM_HOT_HANDLER(122, ZEND_DEFINED, CONST, ANY, CACHE_SLOT)
{
USE_OPLINE
Expand Down
Loading
Loading