diff --git a/NEWS b/NEWS index 26a495a6ddb3..aa813a76dbe1 100644 --- a/NEWS +++ b/NEWS @@ -47,6 +47,8 @@ PHP NEWS . Fixed float literals losing precision in AST export. (Jorg Sowa) . Fixed arrow functions not capturing variables in partial application args. (mihaliak) + . Fixed bug GH-25379 (array_map() foreach optimization changes callback + semantics). (prateekbhujel) - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) diff --git a/Zend/tests/array_map_foreach_optimization_gh25379.phpt b/Zend/tests/array_map_foreach_optimization_gh25379.phpt new file mode 100644 index 000000000000..004ec247bb1d --- /dev/null +++ b/Zend/tests/array_map_foreach_optimization_gh25379.phpt @@ -0,0 +1,259 @@ +--TEST-- +GH-25379: array_map() foreach optimization callee single evaluation and memory leaks on fallback +--FILE-- +getMessage(), "\n"; +} + +/* 2. Non-literal callee with IS_CONST string */ +try { + array_map(getf()(...), "hello"); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 3. Non-literal callee with IS_CONST null */ +try { + array_map(getf()(...), null); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 4. Non-literal callee with IS_CONST bool */ +try { + array_map(getf()(...), false); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 5. Non-literal class with IS_CONST int */ +try { + array_map(getc()::m(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 6. Non-literal class and method with IS_CONST int */ +try { + array_map(getc()::{(getm())}(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 7. Non-existent class with dynamic method and IS_CONST int */ +try { + array_map(Foo::{(getm())}(...), 5); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 8. Exception in dynamic method evaluation */ +try { + array_map(getc()::{(throw_getm())}(...), 5); +} catch (RuntimeException $e) { + echo $e->getMessage(), "\n"; +} + +/* 9. Global constant non-array */ +try { + array_map(getf()(...), CONST_INT); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 10. Class constant non-array */ +try { + array_map(getf()(...), ConstHolder::VAL); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 11. Constant binary expression non-array */ +try { + array_map(getf()(...), 1 + 2); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 12. Variable callable with IS_CONST int */ +$f = 'strtoupper'; +try { + array_map($f(...), 5); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 13. Non-literal callee with __FILE__ */ +try { + array_map(getf()(...), __FILE__); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 14. Non-literal callee with stdClass::class */ +try { + array_map(getf()(...), stdClass::class); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 15. Non-literal callee with true */ +try { + array_map(getf()(...), true); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 16. Non-literal callee with __FILE__ . '.php' */ +try { + array_map(getf()(...), __FILE__ . '.php'); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 17. PFA with multiple placeholders falls back, callee evaluated once */ +try { + var_dump(array_map(getf()(?, ?), [1])); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 18. PFA with variadic placeholder and named args falls back, callee evaluated once */ +try { + var_dump(array_map(getf_named()(a: 1, ...), [2])); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +/* 19. PFA with assert and \assert falls back cleanly */ +var_dump(array_map(assert(...), [1])); +var_dump(array_map(\assert(...), [1])); + +/* 20. Non-interned IS_CONST string produced during expression compilation */ +try { + array_map(getf()(...), "unique_non_interned_" . strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 21. IS_CONST int produced during expression compilation */ +try { + array_map(getf()(...), strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 22. Dynamic static call with non-interned IS_CONST string */ +try { + array_map(getc()::m(...), "unique_non_interned_static_" . strlen("abcdef")); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +/* 23. Exception in dynamic method evaluation on optimized path */ +try { + array_map(getc()::{(throw_getm())}(...), "unique_non_interned_throw_" . strlen("abcdef")); +} catch (RuntimeException $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, null given +getf +array_map(): Argument #2 ($array) must be of type array, false given +getc +array_map(): Argument #2 ($array) must be of type array, int given +getc +getm +array_map(): Argument #2 ($array) must be of type array, int given +getm +Class "Foo" not found +getc +boom m +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, int given +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, true given +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +Partial application of strtoupper() expects at most 1 arguments, 2 given +getf_named +array(1) { + [0]=> + int(3) +} +array(1) { + [0]=> + bool(true) +} +array(1) { + [0]=> + bool(true) +} +getf +array_map(): Argument #2 ($array) must be of type array, string given +getf +array_map(): Argument #2 ($array) must be of type array, int given +getc +array_map(): Argument #2 ($array) must be of type array, string given +getc +boom m diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 61274db1ba7d..f9ea5c1163e9 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5338,12 +5338,12 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg return FAILURE; } - /* Bail out if we do not have exactly two parameters. */ + /* Skip the optimization if we do not have exactly two parameters. */ if (args->children != 2) { return FAILURE; } - /* Bail out if callback is not a first class callable */ + /* Skip the optimization if callback is not a first class callable. */ zend_ast *callback = args->child[0]; if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { return FAILURE; @@ -5374,6 +5374,25 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg } } + /* Skip the optimization if the input array is known at compile time to not + * be an array, before compiling any callee expressions or emitting opcodes. */ + zend_eval_const_expr(&args->child[1]); + if (args->child[1]->kind == ZEND_AST_ZVAL + && Z_TYPE_P(zend_ast_get_zval(args->child[1])) != IS_ARRAY) { + return FAILURE; + } + + znode value; + value.op_type = IS_TMP_VAR; + value.u.op.var = get_temporary_variable(); + + zend_ast *call_args = zend_partial_apply(callback, + zend_ast_create_znode(&value)); + if (!call_args) { + CG(active_op_array)->T--; + return FAILURE; + } + /* Evaluate class name */ znode class_node; if (callback->kind == ZEND_AST_STATIC_CALL @@ -5419,34 +5438,21 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg } } - znode value; - value.op_type = IS_TMP_VAR; - value.u.op.var = get_temporary_variable(); - - zend_ast *call_args = zend_partial_apply(callback, - zend_ast_create_znode(&value)); - if (!call_args) { - goto fail; - } - zend_op *opline; znode array; zend_compile_expr(&array, args->child[1]); - /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */ - if (array.op_type == IS_CONST) { - if (Z_TYPE(array.u.constant) != IS_ARRAY) { - zval_ptr_dtor_nogc(&array.u.constant); - goto fail; - } - Z_TRY_ADDREF(array.u.constant); - } /* 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); + /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. + * Addref after emitting the opline so string literals are interned first. */ + if (opline->op2_type == IS_CONST) { + Z_TRY_ADDREF_P(CT_CONSTANT(opline->op2)); + } opline->lineno = lineno; opline->extended_value = (2 << 16) | IS_ARRAY; const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); @@ -5523,17 +5529,6 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg } return SUCCESS; - -fail: - CG(active_op_array)->T--; - if (func_node.op_type == IS_CONST) { - zval_ptr_dtor_nogc(&func_node.u.constant); - } - if (class_node.op_type == IS_CONST) { - zval_ptr_dtor_nogc(&class_node.u.constant); - } - - return FAILURE; } static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */