From d8eb5886b3e604929a95abb55a8d2eee0aa78756 Mon Sep 17 00:00:00 2001 From: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:50:22 +0200 Subject: [PATCH 1/3] Skip the turbo binaries when this PHP cannot load them A statically linked PHP, such as Laravel Herd Lite, cannot load any extension, and glibc before 2.34 is older than the binaries need. Passing the binary anyway makes PHP print a startup warning on every run. Both cases show in /proc/self/maps, which the musl check already reads. --- src/Turbo/TurboExtensionSelector.php | 31 +++++++++++- .../Turbo/TurboExtensionSelectorTest.php | 47 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/Turbo/TurboExtensionSelector.php b/src/Turbo/TurboExtensionSelector.php index c19c6bc9241..7f3aef7ec54 100644 --- a/src/Turbo/TurboExtensionSelector.php +++ b/src/Turbo/TurboExtensionSelector.php @@ -6,6 +6,7 @@ use function dirname; use function file_get_contents; use function get_cfg_var; +use function in_array; use function is_dir; use function is_file; use function is_string; @@ -176,10 +177,14 @@ private static function getPlatformDirectoryNextToPhar(?string $pharPath = null) return null; } - $platform = self::resolvePlatformDirectory(PHP_OS_FAMILY, php_uname('m'), self::isMusl()); + $selfMaps = @file_get_contents('/proc/self/maps'); + $platform = self::resolvePlatformDirectory(PHP_OS_FAMILY, php_uname('m'), self::resolveIsMusl($selfMaps, is_file('/etc/alpine-release'))); if ($platform === null) { return null; } + if (PHP_OS_FAMILY === 'Linux' && !self::resolveCanLoadLinuxBinaries($selfMaps)) { + return null; + } return sprintf('%s/turbo-ext/%s', dirname($pharPath), $platform); } @@ -233,6 +238,30 @@ public static function isMusl(): bool return self::resolveIsMusl(@file_get_contents('/proc/self/maps'), is_file('/etc/alpine-release')); } + /** + * Whether this PHP can load the Linux binaries at all, judged from what is + * mapped into the running process. Passing one that cannot load makes PHP + * print a startup warning on every run, before PHPStan could react. + * + * A statically linked PHP (Laravel Herd Lite, static-php-cli) maps no + * dynamic loader and cannot load any extension. glibc before 2.34 maps + * its versioned file names (libc-2.31.so), and the binaries need 2.34. + */ + public static function resolveCanLoadLinuxBinaries(string|false $selfMaps): bool + { + if (in_array($selfMaps, [false, ''], true)) { + // nothing to judge from: try loading them as before + return true; + } + + // strpos() rather than str_contains(), see resolveIsMusl() + if (strpos($selfMaps, '/libc-2.') !== false) { + return false; + } + + return strpos($selfMaps, '/ld-linux-') !== false || strpos($selfMaps, '/ld-musl-') !== false; + } + public static function resolveIsMusl(string|false $selfMaps, bool $hasAlpineRelease): bool { if ($selfMaps !== false) { diff --git a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php index 513fdee3b20..8f3420016d2 100644 --- a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php +++ b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php @@ -93,4 +93,51 @@ public function testResolveIsMusl(string|false $selfMaps, bool $hasAlpineRelease $this->assertSame($expected, TurboExtensionSelector::resolveIsMusl($selfMaps, $hasAlpineRelease)); } + /** + * @return iterable + */ + public static function dataResolveCanLoadLinuxBinaries(): iterable + { + // php:8.5-cli (Debian 13, glibc 2.41) + yield [<<<'MAPS' + 556643600000-556643781000 r--p 00000000 00:4b 451272 /usr/local/bin/php + 7f21f0bc9000-7f21f0bf1000 r--p 00000000 00:4b 135364 /usr/lib/x86_64-linux-gnu/libc.so.6 + 7f21f1b79000-7f21f1b7a000 r--p 00000000 00:4b 135344 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 + 7ffca2df7000-7ffca2e18000 rw-p 00000000 00:00 0 [stack] + MAPS, true]; + + // php:8.4-cli-alpine3.23, from the musl case above + yield [<<<'MAPS' + 58adcee00000-58adcef58000 r--p 00000000 00:67 14555462 /usr/local/bin/php + 7934c7f6a000-7934c7f7e000 r--p 00000000 00:67 14554411 /lib/ld-musl-x86_64.so.1 + 7934c800c000-7934c8010000 rw-p 00000000 00:00 0 + MAPS, true]; + + // php:8.3-cli-bullseye (glibc 2.31): the binaries need glibc 2.34 + yield [<<<'MAPS' + 55d57b200000-55d57b321000 r--p 00000000 00:4b 515798 /usr/local/bin/php + 7fb024bbb000-7fb024bdd000 r--p 00000000 00:4b 519011 /lib/x86_64-linux-gnu/libc-2.31.so + 7fb02572c000-7fb02572d000 r--p 00000000 00:4b 519018 /lib/x86_64-linux-gnu/ld-2.31.so + 7ffe763ba000-7ffe763db000 rw-p 00000000 00:00 0 [stack] + MAPS, false]; + + // Laravel Herd Lite PHP 8.5, statically linked: no loader is mapped + yield [<<<'MAPS' + 00400000-0267b000 r-xp 00000000 fe:10 2678858 /home/user/.config/herd-lite/bin/php + 02800000-05ec0000 r-xp 02400000 fe:10 2678858 /home/user/.config/herd-lite/bin/php + 7f6fb0cbb000-7f6fb0cbd000 r-xp 00000000 00:00 0 [vdso] + 7ffd060ab000-7ffd060cc000 rw-p 00000000 00:00 0 [stack] + MAPS, false]; + + // no procfs to inspect + yield [false, true]; + yield ['', true]; + } + + #[DataProvider('dataResolveCanLoadLinuxBinaries')] + public function testResolveCanLoadLinuxBinaries(string|false $selfMaps, bool $expected): void + { + $this->assertSame($expected, TurboExtensionSelector::resolveCanLoadLinuxBinaries($selfMaps)); + } + } From 9e068dabdd32e4bf5b6d187aed4bb5c817ccce99 Mon Sep 17 00:00:00 2001 From: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:29:48 +0200 Subject: [PATCH 2/3] Match the libc and the static layout by mapped file, not by substring Parse each /proc/self/maps line and look at the basename of the mapped file, so a directory name like libc-2.x or ld-musl- in a path no longer decides anything. Old glibc is now a parsed libc version below 2.34. A static PHP is one whose own binary is mapped while no other executable file is (shared memory like a UPX memfd or the opcache JIT buffer aside), instead of a process without a known loader name, so an unfamiliar dynamic layout keeps trying the binaries. --- src/Turbo/TurboExtensionSelector.php | 74 ++++++-- .../Turbo/TurboExtensionSelectorTest.php | 177 ++++++++++++++---- 2 files changed, 197 insertions(+), 54 deletions(-) diff --git a/src/Turbo/TurboExtensionSelector.php b/src/Turbo/TurboExtensionSelector.php index 7f3aef7ec54..e933ad15959 100644 --- a/src/Turbo/TurboExtensionSelector.php +++ b/src/Turbo/TurboExtensionSelector.php @@ -3,16 +3,20 @@ namespace PHPStan\Turbo; use Phar; +use function basename; use function dirname; +use function explode; use function file_get_contents; use function get_cfg_var; -use function in_array; use function is_dir; use function is_file; use function is_string; use function php_uname; +use function preg_match; +use function readlink; use function sprintf; use function strpos; +use function version_compare; use const PHP_DEBUG; use const PHP_MAJOR_VERSION; use const PHP_MINOR_VERSION; @@ -161,7 +165,8 @@ public static function findExtension(?string $pharPath = null): ?string * The directory of turbo-ext/ next to the running phar that holds the * binaries for this platform, whether it exists or not. Null when no * binary is built for this runtime: PHP older than the minimum, a debug - * build, a run from source, or a platform without binaries. + * build, a run from source, or a platform without binaries. Also null + * on Linux when shouldTryLinuxBinaries() finds a reason to skip them. */ private static function getPlatformDirectoryNextToPhar(?string $pharPath = null): ?string { @@ -182,7 +187,7 @@ private static function getPlatformDirectoryNextToPhar(?string $pharPath = null) if ($platform === null) { return null; } - if (PHP_OS_FAMILY === 'Linux' && !self::resolveCanLoadLinuxBinaries($selfMaps)) { + if (PHP_OS_FAMILY === 'Linux' && !self::shouldTryLinuxBinaries($selfMaps, @readlink('/proc/self/exe'))) { return null; } @@ -239,27 +244,64 @@ public static function isMusl(): bool } /** - * Whether this PHP can load the Linux binaries at all, judged from what is - * mapped into the running process. Passing one that cannot load makes PHP - * print a startup warning on every run, before PHPStan could react. + * Whether to try the bundled Linux binaries, judged from the file-backed + * mappings of the running process. True means no reason to skip them was + * found, not that they will load: anything this cannot interpret keeps + * the existing attempt. * - * A statically linked PHP (Laravel Herd Lite, static-php-cli) maps no - * dynamic loader and cannot load any extension. glibc before 2.34 maps - * its versioned file names (libc-2.31.so), and the binaries need 2.34. + * The binaries are skipped when + * - a mapped libc has a versioned name older than 2.34 (libc-2.31.so), + * which glibc used for its own files before 2.34, the version the + * binaries are built against + * - the PHP binary is mapped and no other executable file is, so no + * dynamic loader or shared library is there. The fully static musl + * builds of Laravel Herd Lite and static-php-cli (UPX-packed) look like + * this and fail to load any extension with + * "Dynamic loading not supported". A fully static glibc build looks the + * same and is skipped too, but none was tested. + * + * @param string|false $executable the target of /proc/self/exe */ - public static function resolveCanLoadLinuxBinaries(string|false $selfMaps): bool + public static function shouldTryLinuxBinaries(string|false $selfMaps, string|false $executable): bool { - if (in_array($selfMaps, [false, ''], true)) { - // nothing to judge from: try loading them as before + if ($selfMaps === false) { return true; } - // strpos() rather than str_contains(), see resolveIsMusl() - if (strpos($selfMaps, '/libc-2.') !== false) { - return false; + $executableMapped = false; + $otherExecutableMapped = false; + foreach (explode("\n", $selfMaps) as $line) { + // address perms offset dev inode pathname, the pathname may contain + // spaces and ends with " (deleted)" when the file was replaced, the + // same as the /proc/self/exe target then + if (preg_match('~^\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+(/.*)~', $line, $matches) !== 1) { + continue; + } + + $path = $matches[2]; + if ( + preg_match('~\Alibc-(\d+\.\d+)(?:\.\d+)*\.so(?: \(deleted\))?\z~', basename($path), $versionMatches) === 1 + && version_compare($versionMatches[1], '2.34', '<') + ) { + return false; + } + + if ($path === $executable) { + $executableMapped = true; + continue; + } + + // shared memory shows up as a deleted file: the memfd a UPX-packed + // binary runs from, or the opcache JIT buffer in /dev/zero. A file + // deleted right before this process started would be skipped too. + if (strpos($matches[1], 'x') === false || preg_match('~ \(deleted\)\z~', $path) === 1) { + continue; + } + + $otherExecutableMapped = true; } - return strpos($selfMaps, '/ld-linux-') !== false || strpos($selfMaps, '/ld-musl-') !== false; + return !$executableMapped || $otherExecutableMapped; } public static function resolveIsMusl(string|false $selfMaps, bool $hasAlpineRelease): bool diff --git a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php index 8f3420016d2..606adb65d53 100644 --- a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php +++ b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php @@ -4,6 +4,7 @@ use PHPStan\Testing\PHPStanTestCase; use PHPUnit\Framework\Attributes\DataProvider; +use function str_replace; final class TurboExtensionSelectorTest extends PHPStanTestCase { @@ -94,50 +95,150 @@ public function testResolveIsMusl(string|false $selfMaps, bool $hasAlpineRelease } /** - * @return iterable + * @return iterable */ - public static function dataResolveCanLoadLinuxBinaries(): iterable + public static function dataShouldTryLinuxBinaries(): iterable { - // php:8.5-cli (Debian 13, glibc 2.41) - yield [<<<'MAPS' - 556643600000-556643781000 r--p 00000000 00:4b 451272 /usr/local/bin/php - 7f21f0bc9000-7f21f0bf1000 r--p 00000000 00:4b 135364 /usr/lib/x86_64-linux-gnu/libc.so.6 - 7f21f1b79000-7f21f1b7a000 r--p 00000000 00:4b 135344 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 + // captured with readlink('/proc/self/exe') and /proc/self/maps, one + // line per pathname and permission set + + // php:8.5-cli, Debian 13, glibc 2.41 + $glibc241 = <<<'MAPS' + 55ce84200000-55ce84381000 r--p 00000000 00:51 451272 /usr/local/bin/php + 55ce84400000-55ce84c01000 r-xp 00200000 00:51 451272 /usr/local/bin/php + 55ce85e00000-55ce85ec7000 rw-p 01c00000 00:51 451272 /usr/local/bin/php + 7f72f7e6d000-7f72f7e95000 r--p 00000000 00:51 135364 /usr/lib/x86_64-linux-gnu/libc.so.6 + 7f72f7e95000-7f72f7ff8000 r-xp 00028000 00:51 135364 /usr/lib/x86_64-linux-gnu/libc.so.6 + 7f72f8052000-7f72f8054000 rw-p 001e4000 00:51 135364 /usr/lib/x86_64-linux-gnu/libc.so.6 + 7f72f8e1d000-7f72f8e1e000 r--p 00000000 00:51 135344 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 + 7f72f8e1e000-7f72f8e46000 r-xp 00001000 00:51 135344 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 + 7f72f8e53000-7f72f8e54000 rw-p 00036000 00:51 135344 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7ffca2df7000-7ffca2e18000 rw-p 00000000 00:00 0 [stack] - MAPS, true]; - - // php:8.4-cli-alpine3.23, from the musl case above - yield [<<<'MAPS' - 58adcee00000-58adcef58000 r--p 00000000 00:67 14555462 /usr/local/bin/php - 7934c7f6a000-7934c7f7e000 r--p 00000000 00:67 14554411 /lib/ld-musl-x86_64.so.1 - 7934c800c000-7934c8010000 rw-p 00000000 00:00 0 - MAPS, true]; - - // php:8.3-cli-bullseye (glibc 2.31): the binaries need glibc 2.34 - yield [<<<'MAPS' - 55d57b200000-55d57b321000 r--p 00000000 00:4b 515798 /usr/local/bin/php - 7fb024bbb000-7fb024bdd000 r--p 00000000 00:4b 519011 /lib/x86_64-linux-gnu/libc-2.31.so - 7fb02572c000-7fb02572d000 r--p 00000000 00:4b 519018 /lib/x86_64-linux-gnu/ld-2.31.so - 7ffe763ba000-7ffe763db000 rw-p 00000000 00:00 0 [stack] - MAPS, false]; - - // Laravel Herd Lite PHP 8.5, statically linked: no loader is mapped - yield [<<<'MAPS' - 00400000-0267b000 r-xp 00000000 fe:10 2678858 /home/user/.config/herd-lite/bin/php - 02800000-05ec0000 r-xp 02400000 fe:10 2678858 /home/user/.config/herd-lite/bin/php - 7f6fb0cbb000-7f6fb0cbd000 r-xp 00000000 00:00 0 [vdso] - 7ffd060ab000-7ffd060cc000 rw-p 00000000 00:00 0 [stack] - MAPS, false]; - - // no procfs to inspect - yield [false, true]; - yield ['', true]; + MAPS; + yield 'glibc 2.41' => [$glibc241, '/usr/local/bin/php', true]; + + // php:8.4-cli-alpine + yield 'musl' => [<<<'MAPS' + 56037fc00000-56037fd59000 r--p 00000000 00:51 584944 /usr/local/bin/php + 56037fe00000-560380334000 r-xp 00200000 00:51 584944 /usr/local/bin/php + 560381200000-5603812c4000 rw-p 01400000 00:51 584944 /usr/local/bin/php + 7fcb84616000-7fcb8462a000 r--p 00000000 00:51 585480 /lib/ld-musl-x86_64.so.1 + 7fcb8462a000-7fcb84683000 r-xp 00014000 00:51 585480 /lib/ld-musl-x86_64.so.1 + 7fcb846ba000-7fcb846bb000 rw-p 000a3000 00:51 585480 /lib/ld-musl-x86_64.so.1 + MAPS, '/usr/local/bin/php', true]; + + // php:8.3-cli-bullseye, glibc 2.31 + yield 'glibc 2.31' => [<<<'MAPS' + 55700b000000-55700b121000 r--p 00000000 00:51 515798 /usr/local/bin/php + 55700b200000-55700b661000 r-xp 00200000 00:51 515798 /usr/local/bin/php + 55700c600000-55700c607000 rw-p 01400000 00:51 515798 /usr/local/bin/php + 7fcbc759b000-7fcbc75bd000 r--p 00000000 00:51 519011 /lib/x86_64-linux-gnu/libc-2.31.so + 7fcbc75bd000-7fcbc7716000 r-xp 00022000 00:51 519011 /lib/x86_64-linux-gnu/libc-2.31.so + 7fcbc7769000-7fcbc776b000 rw-p 001cd000 00:51 519011 /lib/x86_64-linux-gnu/libc-2.31.so + 7fcbc810c000-7fcbc810d000 r--p 00000000 00:51 519018 /lib/x86_64-linux-gnu/ld-2.31.so + 7fcbc810d000-7fcbc812d000 r-xp 00001000 00:51 519018 /lib/x86_64-linux-gnu/ld-2.31.so + 7fcbc8137000-7fcbc8138000 rw-p 0002a000 00:51 519018 /lib/x86_64-linux-gnu/ld-2.31.so + MAPS, '/usr/local/bin/php', false]; + + // Laravel Herd Lite PHP 8.5, fully static musl (its path renamed) + $static = <<<'MAPS' + 00400000-0267b000 r-xp 00000000 fe:10 2678858 /opt/herd-lite/bin/php + 02800000-05ec0000 r-xp 02400000 fe:10 2678858 /opt/herd-lite/bin/php + 05ec0000-0621f000 rw-p 05ac0000 fe:10 2678858 /opt/herd-lite/bin/php + 0621f000-06305000 rw-p 00000000 00:00 0 + 43981000-43998000 rw-p 00000000 00:00 0 [heap] + 7f4406333000-7f4406334000 r--s 00000000 00:51 512498 /usr/share/zoneinfo/Etc/UTC + 7f440638d000-7f440638f000 r-xp 00000000 00:00 0 [vdso] + 7ffc3d860000-7ffc3d881000 rw-p 00000000 00:00 0 [stack] + ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall] + MAPS; + yield 'static' => [$static, '/opt/herd-lite/bin/php', false]; + yield 'static, executable unknown' => [$static, false, true]; + yield 'static, executable not in the maps' => [$static, '/usr/bin/php', true]; + + // static-php-cli PHP 8.2 (its path renamed), fully static musl, UPX-packed + $staticUpx = <<<'MAPS' + 00400000-00401000 r--p 00000000 00:00 0 + 00401000-0164c000 r-xs 00000000 00:01 971628 /memfd:upx (deleted) + 0164c000-0251f000 r--p 00000000 00:00 0 + 0251f000-0274e000 rw-p 00000000 00:00 0 + 7f417d3c0000-7f417d3c1000 r--s 00000000 00:32 137257 /usr/share/zoneinfo/Etc/UTC + 7f417d604000-7f417d605000 r--p 00000000 fe:10 2627760 /opt/spc/bin/php + 7f417d605000-7f417d606000 rw-p 00000000 00:00 0 + 7f417d606000-7f417d60a000 r--p 00000000 00:00 0 [vvar] + 7f417d60a000-7f417d60c000 r-xp 00000000 00:00 0 [vdso] + ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall] + MAPS; + yield 'static, UPX-packed' => [$staticUpx, '/opt/spc/bin/php', false]; + yield 'static, UPX-packed, executable unknown' => [$staticUpx, false, true]; + yield 'UPX-packed with a dynamic loader' => [ + $staticUpx . "\n7f417d700000-7f417d728000 r-xp 00001000 00:51 135360 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", + '/opt/spc/bin/php', + true, + ]; + + // the rest are edits of the captures above + $withBinaryAt = static fn (string $maps, string $from, string $to): string => str_replace($from, $to, $maps); + + yield 'static under ld-musl- and ld-linux- directories' => [ + $withBinaryAt($static, '/opt/herd-lite/bin/php', '/opt/ld-musl-tools/ld-linux-x86-64/bin/php'), + '/opt/ld-musl-tools/ld-linux-x86-64/bin/php', + false, + ]; + yield 'static with another executable file mapped' => [ + $static . "\n7f4406300000-7f4406320000 r-xp 00000000 00:51 512499 /opt/herd-lite/lib/preload.so", + '/opt/herd-lite/bin/php', + true, + ]; + yield 'static with a data file mapped from an x86_64 directory' => [ + $static . "\n7f4406300000-7f4406320000 r--p 00000000 00:51 512499 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", + '/opt/herd-lite/bin/php', + false, + ]; + yield 'static with the opcache JIT buffer' => [ + $static . "\n0e400000-12400000 r-xs 08000000 00:01 986386 /dev/zero (deleted)", + '/opt/herd-lite/bin/php', + false, + ]; + yield 'static, binary replaced while running' => [ + $withBinaryAt($static, '/opt/herd-lite/bin/php', '/opt/herd-lite/bin/php (deleted)'), + '/opt/herd-lite/bin/php (deleted)', + false, + ]; + + yield 'glibc 2.41, PHP in a directory named libc-2.x' => [ + $withBinaryAt($glibc241, '/usr/local/bin/php', '/opt/libc-2.project/bin/php'), + '/opt/libc-2.project/bin/php', + true, + ]; + yield 'glibc 2.41 with an unfamiliar loader name' => [ + $withBinaryAt($glibc241, '/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '/opt/rt/custom-loader.so'), + '/usr/local/bin/php', + true, + ]; + yield 'libc 2.31 with a loader that is otherwise accepted' => [ + $withBinaryAt($glibc241, '/usr/lib/x86_64-linux-gnu/libc.so.6', '/usr/lib/x86_64-linux-gnu/libc-2.31.so'), + '/usr/local/bin/php', + false, + ]; + yield 'libc 2.33' => [$withBinaryAt($glibc241, '/libc.so.6', '/libc-2.33.so'), '/usr/local/bin/php', false]; + yield 'libc 2.34' => [$withBinaryAt($glibc241, '/libc.so.6', '/libc-2.34.so'), '/usr/local/bin/php', true]; + yield 'libc 2.41' => [$withBinaryAt($glibc241, '/libc.so.6', '/libc-2.41.so'), '/usr/local/bin/php', true]; + yield 'libc 2.31 replaced while running' => [ + $withBinaryAt($glibc241, '/libc.so.6', '/libc-2.31.so (deleted)'), + '/usr/local/bin/php', + false, + ]; + + yield 'maps not readable' => [false, '/usr/local/bin/php', true]; + yield 'maps empty' => ['', '/usr/local/bin/php', true]; + yield 'maps not interpretable' => ["not\na maps file", '/usr/local/bin/php', true]; } - #[DataProvider('dataResolveCanLoadLinuxBinaries')] - public function testResolveCanLoadLinuxBinaries(string|false $selfMaps, bool $expected): void + #[DataProvider('dataShouldTryLinuxBinaries')] + public function testShouldTryLinuxBinaries(string|false $selfMaps, string|false $executable, bool $expected): void { - $this->assertSame($expected, TurboExtensionSelector::resolveCanLoadLinuxBinaries($selfMaps)); + $this->assertSame($expected, TurboExtensionSelector::shouldTryLinuxBinaries($selfMaps, $executable)); } } From 74bc7c330a5b2a7ab8684c5376f9c9212cf54550 Mon Sep 17 00:00:00 2001 From: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:20:30 +0200 Subject: [PATCH 3/3] Guard readlink and ignore only the observed UPX and JIT mappings Calling readlink() throws on PHP 8+ when the function is disabled, so the lookup now checks function_exists() first and still runs the maps checks with an unknown executable. The static-layout rule no longer treats every (deleted) mapping as shared memory: only the /memfd:upx (deleted) and /dev/zero (deleted) pathnames captured from the tested static-php-cli and Herd Lite JIT runs are skipped. --- src/Turbo/TurboExtensionSelector.php | 41 ++++++++----- .../Turbo/TurboExtensionSelectorTest.php | 57 ++++++++++++++++++- 2 files changed, 83 insertions(+), 15 deletions(-) diff --git a/src/Turbo/TurboExtensionSelector.php b/src/Turbo/TurboExtensionSelector.php index e933ad15959..8b9dd660e25 100644 --- a/src/Turbo/TurboExtensionSelector.php +++ b/src/Turbo/TurboExtensionSelector.php @@ -7,7 +7,9 @@ use function dirname; use function explode; use function file_get_contents; +use function function_exists; use function get_cfg_var; +use function in_array; use function is_dir; use function is_file; use function is_string; @@ -187,8 +189,14 @@ private static function getPlatformDirectoryNextToPhar(?string $pharPath = null) if ($platform === null) { return null; } - if (PHP_OS_FAMILY === 'Linux' && !self::shouldTryLinuxBinaries($selfMaps, @readlink('/proc/self/exe'))) { - return null; + if (PHP_OS_FAMILY === 'Linux') { + $executable = function_exists('readlink') + ? @readlink('/proc/self/exe') + : false; + + if (!self::shouldTryLinuxBinaries($selfMaps, $executable)) { + return null; + } } return sprintf('%s/turbo-ext/%s', dirname($pharPath), $platform); @@ -246,18 +254,22 @@ public static function isMusl(): bool /** * Whether to try the bundled Linux binaries, judged from the file-backed * mappings of the running process. True means no reason to skip them was - * found, not that they will load: anything this cannot interpret keeps - * the existing attempt. + * found, not that they will load. Unreadable or empty maps keep the + * current behaviour. If the executable cannot be identified, the + * static-layout check is skipped; a recognizable old-glibc mapping can + * still be rejected. * * The binaries are skipped when * - a mapped libc has a versioned name older than 2.34 (libc-2.31.so), * which glibc used for its own files before 2.34, the version the * binaries are built against - * - the PHP binary is mapped and no other executable file is, so no - * dynamic loader or shared library is there. The fully static musl - * builds of Laravel Herd Lite and static-php-cli (UPX-packed) look like - * this and fail to load any extension with - * "Dynamic loading not supported". A fully static glibc build looks the + * - the PHP binary is mapped and no other executable file is, matching + * the layouts of the fully static musl builds of Laravel Herd Lite and + * static-php-cli (UPX-packed) tested here, which fail to load any + * extension with "Dynamic loading not supported". Those captures + * include an executable /memfd:upx (deleted) or /dev/zero (deleted) + * mapping (the latter is the opcache JIT buffer), which do not count + * as another executable file. A fully static glibc build looks the * same and is skipped too, but none was tested. * * @param string|false $executable the target of /proc/self/exe @@ -291,10 +303,13 @@ public static function shouldTryLinuxBinaries(string|false $selfMaps, string|fal continue; } - // shared memory shows up as a deleted file: the memfd a UPX-packed - // binary runs from, or the opcache JIT buffer in /dev/zero. A file - // deleted right before this process started would be skipped too. - if (strpos($matches[1], 'x') === false || preg_match('~ \(deleted\)\z~', $path) === 1) { + if (strpos($matches[1], 'x') === false) { + continue; + } + + // executable-memory mappings observed in the tested UPX and + // OPcache builds, not every unlinked backing file + if (in_array($path, ['/memfd:upx (deleted)', '/dev/zero (deleted)'], true)) { continue; } diff --git a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php index 606adb65d53..d3541ab7b1b 100644 --- a/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php +++ b/tests/PHPStan/Turbo/TurboExtensionSelectorTest.php @@ -4,7 +4,15 @@ use PHPStan\Testing\PHPStanTestCase; use PHPUnit\Framework\Attributes\DataProvider; +use function escapeshellarg; +use function exec; +use function implode; +use function sprintf; use function str_replace; +use function var_export; +use const PHP_BINARY; +use const PHP_DEBUG; +use const PHP_OS_FAMILY; final class TurboExtensionSelectorTest extends PHPStanTestCase { @@ -118,14 +126,15 @@ public static function dataShouldTryLinuxBinaries(): iterable yield 'glibc 2.41' => [$glibc241, '/usr/local/bin/php', true]; // php:8.4-cli-alpine - yield 'musl' => [<<<'MAPS' + $musl = <<<'MAPS' 56037fc00000-56037fd59000 r--p 00000000 00:51 584944 /usr/local/bin/php 56037fe00000-560380334000 r-xp 00200000 00:51 584944 /usr/local/bin/php 560381200000-5603812c4000 rw-p 01400000 00:51 584944 /usr/local/bin/php 7fcb84616000-7fcb8462a000 r--p 00000000 00:51 585480 /lib/ld-musl-x86_64.so.1 7fcb8462a000-7fcb84683000 r-xp 00014000 00:51 585480 /lib/ld-musl-x86_64.so.1 7fcb846ba000-7fcb846bb000 rw-p 000a3000 00:51 585480 /lib/ld-musl-x86_64.so.1 - MAPS, '/usr/local/bin/php', true]; + MAPS; + yield 'musl' => [$musl, '/usr/local/bin/php', true]; // php:8.3-cli-bullseye, glibc 2.31 yield 'glibc 2.31' => [<<<'MAPS' @@ -229,6 +238,30 @@ public static function dataShouldTryLinuxBinaries(): iterable '/usr/local/bin/php', false, ]; + yield 'glibc 2.41, libc and loader replaced while running' => [ + $withBinaryAt($withBinaryAt( + $glibc241, + '/usr/lib/x86_64-linux-gnu/libc.so.6', + '/usr/lib/x86_64-linux-gnu/libc.so.6 (deleted)', + ), '/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 (deleted)'), + '/usr/local/bin/php', + true, + ]; + yield 'musl, runtime replaced while running' => [ + $withBinaryAt($musl, '/lib/ld-musl-x86_64.so.1', '/lib/ld-musl-x86_64.so.1 (deleted)'), + '/usr/local/bin/php', + true, + ]; + yield 'static with a deleted custom loader' => [ + $static . "\n7f4406300000-7f4406320000 r-xp 00000000 00:51 512499 /opt/rt/custom-loader.so (deleted)", + '/opt/herd-lite/bin/php', + true, + ]; + yield 'static with an unfamiliar deleted memfd' => [ + $static . "\n7f4406300000-7f4406320000 r-xp 00000000 00:00 0 /memfd:unknown-runtime (deleted)", + '/opt/herd-lite/bin/php', + true, + ]; yield 'maps not readable' => [false, '/usr/local/bin/php', true]; yield 'maps empty' => ['', '/usr/local/bin/php', true]; @@ -241,4 +274,24 @@ public function testShouldTryLinuxBinaries(string|false $selfMaps, string|false $this->assertSame($expected, TurboExtensionSelector::shouldTryLinuxBinaries($selfMaps, $executable)); } + public function testFindExtensionDoesNotErrorWhenReadlinkIsDisabled(): void + { + if (PHP_OS_FAMILY !== 'Linux' || (bool) PHP_DEBUG) { + $this->markTestSkipped('the readlink() call only runs on a non-debug Linux PHP'); + } + + $code = sprintf( + 'require %s; echo function_exists("readlink") ? "defined" : json_encode(\\PHPStan\\Turbo\\TurboExtensionSelector::findExtension("/tmp/nonexistent/phpstan.phar"));', + var_export(__DIR__ . '/../../../src/Turbo/TurboExtensionSelector.php', true), + ); + exec(sprintf( + '%s -d disable_functions=readlink -r %s', + escapeshellarg(PHP_BINARY), + escapeshellarg($code), + ), $output, $exitCode); + + $this->assertSame(0, $exitCode, implode("\n", $output)); + $this->assertSame('null', implode('', $output)); + } + }