Skip to content

Skip pipe/array_map optimization for forbid-dynamic-calls functions - #25672

Open
prateekbhujel wants to merge 3 commits into
php:PHP-8.6from
prateekbhujel:fix/forbid-dynamic-calls-partial-apply
Open

prateekbhujel wants to merge 3 commits into
php:PHP-8.6from
prateekbhujel:fix/forbid-dynamic-calls-partial-apply

Conversation

@prateekbhujel

@prateekbhujel prateekbhujel commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #25546 for the extract() / compact() / func_get_arg() case in GH-25379.

When zend_partial_apply() optimizes an FCC/PFA in array_map() or |>, rewriting it into a direct call skips the dynamic call check so functions flagged with ZEND_ACC2_FORBID_DYN_CALLS end up running directly in the caller's scope instead of throwing Error: Cannot call ...() dynamically.

This checks ZEND_ACC2_FORBID_DYN_CALLS in zend_is_pipe_optimizable_callable_name() (covering resolved function names, aliases, and namespace fallback to global functions). Resolving the function name there also avoids hitting the assert() assertion failure when assert is imported under an alias like use function assert as a;.

/cc @arnaud-lb

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().
Comment thread Zend/zend_compile.c Outdated
return !fbc || !(fbc->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS);
}

static bool zend_is_pipe_optimizable_method_name(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

Fair enough, dropped the method call check and the zend_test test in 0a9510d, just checking function names now.

Comment thread Zend/zend_compile.c Outdated

/* 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

!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.

Comment thread Zend/zend_compile.c

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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"
}

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.

Good catch — updated in 8c99542:

  • zend_compile_dynamic_call() now emits ZEND_INIT_DYNAMIC_CALL for dynamic calls whose constant string name resolves to a ZEND_ACC2_FORBID_DYN_CALLS function (passing is_dynamic so direct calls falling back through zend_compile_ignore_function() still emit ZEND_INIT_FCALL_BY_NAME).
  • Enabled the IS_STRING branch for OP2_TYPE == IS_CONST in ZEND_INIT_DYNAMIC_CALL (Zend/zend_vm_def.h + regenerated Zend/zend_vm_execute.h) so const-string dynamic calls go through zend_init_dynamic_call_string() instead of throwing Value of type string is not callable.
  • Added your const-expression regression test (plus \extract, uppercase EXTRACT, direct const calls, and pipe cases) in Zend/tests/array_map_foreach_optimization_forbid_dynamic_calls_const_expr.phpt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants