From d2a0a808f03d5e5b8a4b60af83a50563e7801082 Mon Sep 17 00:00:00 2001 From: sji Date: Tue, 30 Apr 2024 01:43:51 +0900 Subject: [PATCH] Fix the auto ZTS detection to use zend_module_entry --- .../TargetPhpSettings/TargetPhpSettings.php | 3 ++- .../ProcessModuleSymbolReaderCreator.php | 3 +++ .../Types/Zend/ZendModuleEntry.php | 7 +++++++ src/Lib/PhpProcessReader/PhpGlobalsFinder.php | 3 +++ .../PhpProcessReader/PhpVersionDetector.php | 18 +++++++++++------- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php b/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php index e6f1bc5e..12dbda7b 100644 --- a/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php +++ b/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php @@ -33,7 +33,8 @@ public function __construct( public string $zts_globals_regex = self::ZTS_GLOBALS_REGEX_DEFAULT, public string $php_version = self::TARGET_PHP_VERSION_DEFAULT, public ?string $php_path = null, - public ?string $libpthread_path = null + public ?string $libpthread_path = null, + public bool $zts = false, ) { } diff --git a/src/Lib/Elf/Process/ProcessModuleSymbolReaderCreator.php b/src/Lib/Elf/Process/ProcessModuleSymbolReaderCreator.php index e6674450..d750f73b 100644 --- a/src/Lib/Elf/Process/ProcessModuleSymbolReaderCreator.php +++ b/src/Lib/Elf/Process/ProcessModuleSymbolReaderCreator.php @@ -53,6 +53,9 @@ public function createModuleReaderByNameRegex( $module_name = $module_memory_map->getModuleName(); $path = $binary_path ?? $this->process_path_resolver->resolve($pid, $module_name); + if (file_exists($path) === false) { + return null; + } $symbol_resolver = new Elf64CachedSymbolResolver( new Elf64LazyParseSymbolResolver( diff --git a/src/Lib/PhpInternals/Types/Zend/ZendModuleEntry.php b/src/Lib/PhpInternals/Types/Zend/ZendModuleEntry.php index 4f7ebc72..a297750d 100644 --- a/src/Lib/PhpInternals/Types/Zend/ZendModuleEntry.php +++ b/src/Lib/PhpInternals/Types/Zend/ZendModuleEntry.php @@ -80,4 +80,11 @@ public function getPointer(): Pointer { return $this->pointer; } + + + + public function isZts(): bool + { + return $this->zts; + } } diff --git a/src/Lib/PhpProcessReader/PhpGlobalsFinder.php b/src/Lib/PhpProcessReader/PhpGlobalsFinder.php index d7bfd5ce..051c6540 100644 --- a/src/Lib/PhpProcessReader/PhpGlobalsFinder.php +++ b/src/Lib/PhpProcessReader/PhpGlobalsFinder.php @@ -44,6 +44,9 @@ public function findTsrmLsCache( ProcessSpecifier $process_specifier, TargetPhpSettings $target_php_settings ): ?int { + if (!$target_php_settings->zts) { + return null; + } $tsrm_ls_cache_cdata = $this->getSymbolReader( $process_specifier, $target_php_settings diff --git a/src/Lib/PhpProcessReader/PhpVersionDetector.php b/src/Lib/PhpProcessReader/PhpVersionDetector.php index 084fa813..657542f4 100644 --- a/src/Lib/PhpProcessReader/PhpVersionDetector.php +++ b/src/Lib/PhpProcessReader/PhpVersionDetector.php @@ -75,16 +75,19 @@ public function decidePhpVersion( $process_specifier, $target_php_settings, ); - $version = null; + $version_and_zts = null; if (!is_null($module_registry_address)) { - $version = $this->detectPhpVersion($process_specifier->pid, $module_registry_address); + $version_and_zts = $this->detectPhpVersion($process_specifier->pid, $module_registry_address); } - if (is_null($version)) { + if (is_null($version_and_zts)) { /** @var value-of $version */ $version = 'v' . PHP_MAJOR_VERSION . PHP_MINOR_VERSION; Assert::true(ZendTypeReader::isSupported($version)); - } + $is_zts = false; + } else { + [$version, $is_zts] = $version_and_zts; + } return new TargetPhpSettings( php_regex: $target_php_settings->php_regex, @@ -93,14 +96,15 @@ public function decidePhpVersion( php_version: $version, php_path: $target_php_settings->php_path, libpthread_path: $target_php_settings->libpthread_path, + zts: $is_zts, ); } - /** @return value-of|null */ + /** @return array{value-of, bool}|null */ public function detectPhpVersion( int $pid, int $module_registry_address - ): ?string { + ): ?array { $fake_php_version = ZendTypeReader::V70; $dereferencer = $this->getDereferencer($pid, $fake_php_version); $module_registry = $this->getModuleRegistry( @@ -127,7 +131,7 @@ public function detectPhpVersion( $result_string = 'v' . str_replace('.', '', $version); if (ZendTypeReader::isSupported($result_string)) { /** @var value-of */ - return $result_string; + return [$result_string, $basic_module_entry->isZts()]; } return null; }