Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php -z does not seem to work at all #17740

Open
BenMorel opened this issue Feb 8, 2025 · 4 comments
Open

php -z does not seem to work at all #17740

BenMorel opened this issue Feb 8, 2025 · 4 comments

Comments

@BenMorel
Copy link
Contributor

BenMorel commented Feb 8, 2025

Description

The examples below use the following test.php script:

<?= extension_loaded('Zend OPcache') ? "OPcache loaded\n" : "OPcache NOT loaded\n";

The -d zend_extension option accepts either a file name with or without the .so extension, or an absolute path:

$ php -n -d zend_extension=opcache test.php
OPcache loaded
$ php -n -d zend_extension=opcache.so test.php
OPcache loaded
$ php -n -d zend_extension=/usr/lib/php/20230831/opcache.so test.php
OPcache loaded

The -z option, on the other hand, does not seem to accept a file name:

$ php -n -z opcache test.php
Failed loading opcache:  opcache: cannot open shared object file: No such file or directory
OPcache NOT loaded
$ php -n -z opcache.so test.php
Failed loading opcache.so:  opcache.so: cannot open shared object file: No such file or directory
OPcache NOT loaded

It does not complain when passing an absolute path, but still does not load the extension:

$ php -n -z /usr/lib/php/20230831/opcache.so test.php
OPcache NOT loaded

Furthermore, its behaviour becomes really strange when you specify both -d zend_extension and -z:

$ php -n -d zend_extension=opcache -z opcache test.php
Failed loading opcache:  opcache: cannot open shared object file: No such file or directory
OPcache loaded
$ php -n -d zend_extension=opcache.so -z opcache.so test.php
Cannot load Zend OPcache - it was already loaded
OPcache loaded
$ php -n -d zend_extension=/usr/lib/php/20230831/opcache.so -z /usr/lib/php/20230831/opcache.so test.php
Cannot load Zend OPcache - it was already loaded
OPcache loaded

In the last 2 runs, it now says that the extension was already loaded, even though specifying -z alone did not load the extension.

Is the -z argument broken? Should it be removed from php, as it's redundant with -d zend_extension anyway?

PHP Version

PHP 8.1, 8.2, 8.3, 8.4

Operating System

Ubuntu 24.04

@nielsdos
Copy link
Member

nielsdos commented Feb 8, 2025

So the extension gets loaded but it's never registered in the module registry strangely, and so never initialized either...

@nielsdos
Copy link
Member

nielsdos commented Feb 8, 2025

Oh this happens because parsing the -z argument happens in do_cli that happens after module_startup, which is too late.

@nielsdos
Copy link
Member

nielsdos commented Feb 8, 2025

Very dirty workaround PoC that demonstrates this is indeed the problem: https://gist.github.com/nielsdos/52fcc29433d568dd41399ad0c916b129
Question is how to do this cleanly without breaking ABI either...

Another idea is to use the ini builder, which is cleaner, but I wonder how that interacts with escape chars etc...:

diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index 64fb9c5c515..31c96869ea4 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -819,9 +819,6 @@ static int do_cli(int argc, char **argv) /* {{{ */
 				behavior=PHP_MODE_STRIP;
 				break;
 
-			case 'z': /* load extension file */
-				zend_load_extension(php_optarg);
-				break;
 			case 'H':
 				hide_argv = true;
 				break;
@@ -1283,6 +1280,9 @@ int main(int argc, char *argv[])
 			case 'e': /* enable extended info output */
 				use_extended_info = 1;
 				break;
+			case 'z': /* load extension file */
+				php_ini_builder_quoted(&ini_builder, ZEND_STRL("zend_extension"), php_optarg, strlen(php_optarg));
+				break;
 		}
 	}
 exit_loop:

@BenMorel
Copy link
Contributor Author

BenMorel commented Feb 8, 2025

@nielsdos Shouldn’t -z be removed altogether?

Looks like nobody is relying on it, and there is already an alternative with -d, making it redundant.

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

No branches or pull requests

2 participants