Repository navigation
Skip pipe/array_map optimization for forbid-dynamic-calls functions - #25672
prateekbhujel wants to merge 3 commits into
Conversation
When zend_partial_apply() rewrites an FCC/PFA in array_map() or |> into a direct call, functions and static methods flagged with ZEND_ACC2_FORBID_DYN_CALLS bypass zend_forbid_dynamic_call() and run in the caller's scope (e.g. extract(), compact(), func_get_arg()). Resolve the callable name in zend_is_pipe_optimizable_callable() and skip the optimization if it targets a ZEND_ACC2_FORBID_DYN_CALLS function or static method (including namespace fallback and aliases). Resolving the name also fixes aliased assert() hitting an assertion failure in zend_ast_export_ex().
| return !fbc || !(fbc->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS); | ||
| } | ||
|
|
||
| static bool zend_is_pipe_optimizable_method_name( |
There was a problem hiding this comment.
We can ignore method calls for now: We do not have any methods like that except in ext/zend_test and we can not implement this check reliably as we don't generally know the CE.
There was a problem hiding this comment.
Fair enough, dropped the method call check and the zend_test test in 0a9510d, just checking function names now.
|
|
||
| /* Functions that forbid dynamic calls must not be rewritten into direct calls. */ | ||
| const zend_function *fbc = zend_hash_find_ptr_lc(CG(function_table), name); | ||
| return !fbc || !(fbc->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS); |
There was a problem hiding this comment.
This is too conservative as this will prevent optimization for any function that is not yet defined.
We can make the assumption that if a function is not defined yet, the it's not ZEND_ACC2_FORBID_DYN_CALLS, as only internal functions have this flag.
There was a problem hiding this comment.
!fbc || !(fbc->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS) already returned true for undefined functions (fbc == NULL), but the double negation was easy to misread. Rewrote it via a zend_func_forbids_dynamic_calls() helper in 8c99542 so that assumption is explicit.
|
|
||
| static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast) | ||
| { | ||
| if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) { |
There was a problem hiding this comment.
Unfortunately it's possible that a non-ZEND_AST_ZVAL node ends up being evaluated/folded to a const at compile time:
class C {
const EX = 'extract';
static function test() {
// C::EX is evaluated to 'extract' by zend_eval_const_expr()
array_map((C::EX)(...), [['a']]);
}
static function test2() {
// (chr(101) . 'xtract' is folded to 'extract' by zend_compile_binary_op()
array_map((chr(101) . 'xtract')(...), [['a']]);
}
}Both cases are accepted by zend_compile_func_array_map(). The function call is still compiled by zend_compile_dynamic_call(), but it emits ZEND_INIT_FCALL_BY_NAME when the name is CONST.
I would recommend to change zend_compile_dynamic_call() so that it compiles to ZEND_INIT_DYNAMIC_CALL when the function is a ZEND_ACC2_FORBID_DYN_CALLS. Then we do not have to care about this in zend_compile_func_array_map() / zend_is_pipe_optimizable_func_name().
--TEST--
array_map(): foreach optimization does not bypass forbid-dynamic-calls when the callable name is a compile-time constant expression
--FILE--
<?php
class A {
const EXTRACT = 'extract';
const COMPACT = 'compact';
const FUNC_GET_ARG = 'func_get_arg';
const STRTOUPPER = 'strtoupper';
public static function fcc(): void {
try {
array_map((self::EXTRACT)(...), [['zz' => 1]]);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($zz ?? 'unset');
}
public static function pfa(): void {
try {
array_map((self::EXTRACT)(?), [['zz' => 1]]);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($zz ?? 'unset');
}
public static function compact(): void {
$x = 5;
try {
var_dump(array_map((self::COMPACT)(...), ['x']));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}
public static function args($a, $b): void {
try {
var_dump(array_map((self::FUNC_GET_ARG)(...), [1, 0]));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
try {
var_dump(array_map((self::FUNC_GET_ARG)(?), [1, 0]));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}
public static function allowed(): void {
var_dump(array_map((self::STRTOUPPER)(...), ['abc']));
var_dump(array_map((self::STRTOUPPER)(?), ['abc']));
}
}
function test_class_const(): void {
try {
array_map((A::EXTRACT)(...), [['zz' => 1]]);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($zz ?? 'unset');
}
function test_folded_call(): void {
try {
array_map((chr(101) . 'xtract')(...), [['zz' => 1]]);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($zz ?? 'unset');
try {
array_map((chr(101) . 'xtract')(?), [['zz' => 1]]);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($zz ?? 'unset');
var_dump(array_map((chr(115) . 'trtoupper')(...), ['abc']));
}
echo "# A::fcc():\n";
A::fcc();
echo "# A::pfa():\n";
A::pfa();
echo "# A::compact():\n";
A::compact();
echo "# A::args():\n";
A::args('A', 'B');
echo "# A::allowed():\n";
A::allowed();
echo "# test_class_const():\n";
test_class_const();
echo "# test_folded_call():\n";
test_folded_call();
?>
--EXPECT--
# A::fcc():
Error: Cannot call extract() dynamically
string(5) "unset"
# A::pfa():
Error: Cannot call extract() dynamically
string(5) "unset"
# A::compact():
Error: Cannot call compact() dynamically
# A::args():
Error: Cannot call func_get_arg() dynamically
Error: Cannot call func_get_arg() dynamically
# A::allowed():
array(1) {
[0]=>
string(3) "ABC"
}
array(1) {
[0]=>
string(3) "ABC"
}
# test_class_const():
Error: Cannot call extract() dynamically
string(5) "unset"
# test_folded_call():
Error: Cannot call extract() dynamically
string(5) "unset"
Error: Cannot call extract() dynamically
string(5) "unset"
array(1) {
[0]=>
string(3) "ABC"
}There was a problem hiding this comment.
Good catch — updated in 8c99542:
zend_compile_dynamic_call()now emitsZEND_INIT_DYNAMIC_CALLfor dynamic calls whose constant string name resolves to aZEND_ACC2_FORBID_DYN_CALLSfunction (passingis_dynamicso direct calls falling back throughzend_compile_ignore_function()still emitZEND_INIT_FCALL_BY_NAME).- Enabled the
IS_STRINGbranch forOP2_TYPE == IS_CONSTinZEND_INIT_DYNAMIC_CALL(Zend/zend_vm_def.h+ regeneratedZend/zend_vm_execute.h) so const-string dynamic calls go throughzend_init_dynamic_call_string()instead of throwingValue of type string is not callable. - Added your const-expression regression test (plus
\extract, uppercaseEXTRACT, direct const calls, and pipe cases) inZend/tests/array_map_foreach_optimization_forbid_dynamic_calls_const_expr.phpt.
Follow-up to #25546 for the
extract()/compact()/func_get_arg()case in GH-25379.When
zend_partial_apply()optimizes an FCC/PFA inarray_map()or|>, rewriting it into a direct call skips the dynamic call check so functions flagged withZEND_ACC2_FORBID_DYN_CALLSend up running directly in the caller's scope instead of throwingError: Cannot call ...() dynamically.This checks
ZEND_ACC2_FORBID_DYN_CALLSinzend_is_pipe_optimizable_callable_name()(covering resolved function names, aliases, and namespace fallback to global functions). Resolving the function name there also avoids hitting theassert()assertion failure whenassertis imported under an alias likeuse function assert as a;./cc @arnaud-lb