[php-src] PR #25898: Fix GH-25855: Partial application of an internal function via Closure::fromCallable() reuses a stale cached signature
| From: | shuhuNB515 | Date: | Sun, 29 Nov 2026 12:12:25 +0000 |
| Subject: | [php-src] PR #25898: Fix GH-25855: Partial application of an internal function via Closure::fromCallable() reuses a stale cached signature | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41193@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25898
Author: shuhuNB515
Fixes GH-25855
## Problem
```php
<?php
function pfa($c) { return $c(?, 2); }
$c = Closure::fromCallable('str_repeat');
$p = pfa($c);
unset($p, $c);
$c = Closure::fromCallable('round');
$p = pfa($c);
echo new ReflectionFunction($p)->getParameters()[0], "\n";
var_dump($p(1.2345));
```
Output before this patch (debug and release, with or without opcache):
```
Parameter #0 [ <required> string ]
string(4) 1.23
```
The PFA of
round() gets the signature of str_repeat(), and under
strict_types the call throws a TypeError. With opcache the stale entry
also persists in SHM across requests.
## Root cause
Closures wrapping internal functions (as produced by Closure::fromCallable(), including
fake closures) and call trampolines do not use a persistent zend_function:
zend_create_closure_ex() copies the function into the closure object
(memcpy(&closure->func, ...)), so call->func points into the
closure object and its lifetime ends with it.
Both PFA caches assume the pointer is stable and keyed on identity:
- zp_get_op_array() compares cache_slot[0] == function;
- zend_accel_pfa_key() hashes the raw pointer for internal functions (only the
user-function case was special-cased to use opcodes, see the comment there).
When the first closure is freed and a second closure is allocated at the same address, the pointer
comparison falsely hits and the compiled signature of the first function is reused.
## Fix
Add zend_pfa_function_is_cacheable() in Zend/zend_partial.h, which returns
false for internal functions flagged ZEND_ACC_CLOSURE or
ZEND_ACC_CALL_VIA_TRAMPOLINE (their pointers are ephemeral), and use it to:
- skip the cache slot lookup and population in zp_get_op_array();
- skip the zend_accel_pfa_cache_get() lookup and the
EG(partial_function_application_cache) / SHM insertion in
zend_accel_compile_pfa().
Such PFAs are always compiled fresh. In the non-cached path the reference that would have been held
by the cache is handed over to the caller, so the compiled op_array is destroyed together with the
partial closure (this also fixes memory leaks the naive skip would have introduced).
Persistent pointers (internal functions from the function table, user-function opcodes) keep using
the cache unchanged.
## Verification
| Step | Result |
| --- | --- |
| Unpatched build + repro | Parameter #0 [ <required> string $string ] /
string(4) "1.23" (bug) |
| Patched build + repro | Parameter #0 [ <required> int|float $num ] /
float(1.23) (expected output from the issue) |
| Patched build + repro with opcache.enable_cli=1 | expected output |
| New test Zend/tests/partial_application/gh25855.phpt | passes |
| make test TESTS="Zend/tests ext/reflection" | 6140 passed, 0 failed |
| make test TESTS="ext/opcache/tests" | 1055 passed, 0 failed |
| Memory leak check (debug build) | no leaks for single and repeated ephemeral PFAs |