array_map() foreach optimization changes callback semantics (double evaluation/leak, by-ref callbacks, late callback validation, extract/compact scope)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
調査の方向性
Start with zend_compile_func_array_map() in the PHP compiler, introduced by a3576bddc52 / GH-20934. Reproduce the four cases with opcache disabled, then trace the optimization’s bailout path, loop callback invocation, and callback resolution against runtime array_map() behavior. Done means the optimized path preserves evaluation count and order, by-reference warnings, callback validation, and dynamic-call restrictions; add regression tests for the reported cases.
索引モデルが issue の本文から書いたものです。
説明
Description
The compile-time optimization that turns array_map() with a first-class callable callback into a foreach loop (zend_compile_func_array_map(), introduced by a3576bddc52 / GH-20934) diverges from the runtime array_map() in several ways. All cases below reproduce with opcache off, on, and with tracing JIT, and none of them are mentioned in UPGRADING.
1. Non-literal callee is evaluated twice and leaked when the optimization bails out
If the callee is a non-literal expression (getf()(...), $f(...), getc()::m(...)), it is compiled into a TMP before the optimization decides to bail out (e.g. the array argument is a non-array constant). The fail: path only frees CONST nodes, so the TMP's opcodes remain, the TMP is never freed, and the regular call evaluates the expression again.
<?php
function getf() { echo "getf\n"; return str_repeat('strto', 1) . 'upper'; }
try { array_map(getf()(...), 5); } catch (TypeError $e) { echo $e->getMessage(), "\n"; }
8.6 (debug build, opcache off):
getf
getf
array_map(): Argument #2 ($array) must be of type array, int given
Zend/zend_string.h(192) : Freeing 0x00000001088915a0 (40 bytes), script=t.php
=== Total 1 memory leaks detected ===
8.5.12:
getf
array_map(): Argument #2 ($array) must be of type array, int given
array_map(getc()::m(...), 5) with getc() returning an object behaves the same way and leaks the object (Zend/zend_objects.c(190)). The double evaluation reproduces in all opcache modes; the leak is reported with opcache off.
2. By-ref callbacks throw Error instead of a warning
The loop body sends the FE_FETCH_R TMP directly to the callee, so a by-reference parameter throws instead of the warning the runtime array_map() emits. This affects user functions, internal functions (sort(...), array_push(...), settype(...)), and by-ref variadics (&...$a).
<?php
function inc(&$x) { $x++; return $x; }
try { var_dump(array_map(inc(...), [1])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
try { var_dump(array_map(sort(...), [[3, 1]])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
8.6:
Error: inc(): Argument #1 ($x) could not be passed by reference
Error: sort(): Argument #1 ($array) could not be passed by reference
8.5.12:
Warning: inc(): Argument #1 ($x) must be passed by reference, value given in t.php on line 3
array(1) {
[0]=>
int(2)
}
Warning: sort(): Argument #1 ($array) must be passed by reference, value given in t.php on line 4
array(1) {
[0]=>
bool(true)
}
Uncaught, this turns code that worked with a warning in 8.5 into a fatal error.
3. Invalid callbacks are not reported for empty arrays, and are reported after the array is evaluated
8.5 creates the closure first, so an undefined function, missing class, or inaccessible method throws before the array argument is evaluated. The optimized loop resolves the callee only inside the loop body.
<?php
function arr() { echo "arr\n"; return [1]; }
try { var_dump(array_map(nope(...), [])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
try { var_dump(array_map(nope(...), arr())); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
class A { private static function p($x) { return $x; } }
try { var_dump(array_map(A::p(...), [])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
8.6:
array(0) {
}
arr
Error: Call to undefined function nope()
array(0) {
}
8.5.12:
Error: Call to undefined function nope()
Error: Call to undefined function nope()
Error: Call to private method A::p() from global scope
array_map(Nope::m(...), []) (missing class) also returns [] silently instead of throwing Error: Class "Nope" not found.
4. extract()/compact()/func_get_arg() run in the caller's scope
A literal callee is compiled as an ordinary direct call, which skips the "Cannot call X() dynamically" restriction. The scope-introspection functions now operate on the function that calls array_map().
<?php
function f() {
try { array_map(extract(...), [['zz' => 1]]); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
var_dump($zz ?? 'unset');
$x = 5;
try { var_dump(array_map(compact(...), ['x'])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
}
function h($a, $b) {
try { var_dump(array_map(func_get_arg(...), [1, 0])); } catch (Throwable $e) { echo $e::class, ": ", $e->getMessage(), "\n"; }
}
f();
h('A', 'B');
8.6:
int(1)
array(1) {
[0]=>
array(1) {
["x"]=>
int(5)
}
}
array(2) {
[0]=>
string(1) "B"
[1]=>
string(1) "A"
}
8.5.12:
Error: Cannot call extract() dynamically
string(5) "unset"
Error: Cannot call compact() dynamically
Error: Cannot call func_get_arg() dynamically
The pipe operator already behaves this way in 8.5 (['zz' => 1] |> extract(...)), so this one may be considered acceptable, but it is a behavior change for array_map().
Possible direction
Cases 2–4 could be avoided by bailing out when the callee is known to have by-ref parameters or to be one of the dynamic-call-forbidden functions, and by resolving the callee before the loop. Case 1 needs the bail-out checks moved before the callee is compiled, or the emitted opcodes/TMP cleaned up on failure.
PHP Version
PHP 8.6.1-dev (cli) (NTS DEBUG), PHP-8.6 @ dfc3219f341
Operating System
macOS (arm64)
- 主要言語
- C
- スター
- 41.8k
- フォーク
- 8.6k
- 平均マージ
- 2日 15時間
- マージ済み PR(30日)
- 125
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
php/php-src のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)オープンExtension: openssl Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
php/php-src#25402 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
php/php-src#25397 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
SNMP accepts malformed remote ports対応中かも @somethingwithproof が 21 日前に担当しました。 オープン
難易度 2/5 半日 初心者へのやさしさ 62/100
php/php-src#25137 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
AcademySoftwareFoundation/openexr#2884 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
python-pillow/Pillow#10590 ·
メンテナーはふだん 1 日以内に返信
-
status: needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
openwrt/packages#31407 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信