Skip to content

Fix GH-9921: Loading ext in FPM config does not register module handlers #12377

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,8 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
post_deactivate_count++;
}
} ZEND_HASH_FOREACH_END();
module_request_startup_handlers = (zend_module_entry**)malloc(
module_request_startup_handlers = (zend_module_entry**)realloc(
module_request_startup_handlers,
sizeof(zend_module_entry*) *
(startup_count + 1 +
shutdown_count + 1 +
Expand Down Expand Up @@ -2303,7 +2304,8 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
}
} ZEND_HASH_FOREACH_END();

class_cleanup_handlers = (zend_class_entry**)malloc(
class_cleanup_handlers = (zend_class_entry**)realloc(
class_cleanup_handlers,
sizeof(zend_class_entry*) *
(class_count + 1));
class_cleanup_handlers[class_count] = NULL;
Expand All @@ -2329,7 +2331,9 @@ ZEND_API void zend_startup_modules(void) /* {{{ */
ZEND_API void zend_destroy_modules(void) /* {{{ */
{
free(class_cleanup_handlers);
class_cleanup_handlers = NULL;
free(module_request_startup_handlers);
module_request_startup_handlers = NULL;
zend_hash_graceful_reverse_destroy(&module_registry);
}
/* }}} */
Expand Down
25 changes: 24 additions & 1 deletion ext/dl_test/dl_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ PHP_MINIT_FUNCTION(dl_test)
REGISTER_INI_ENTRIES();
}

if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
fprintf(stderr, "DL TEST MINIT\n");
}

return SUCCESS;
}
/* }}} */
Expand All @@ -83,6 +87,10 @@ static PHP_MSHUTDOWN_FUNCTION(dl_test)
UNREGISTER_INI_ENTRIES();
}

if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
fprintf(stderr, "DL TEST MSHUTDOWN\n");
}

return SUCCESS;
}
/* }}} */
Expand All @@ -94,6 +102,21 @@ PHP_RINIT_FUNCTION(dl_test)
ZEND_TSRMLS_CACHE_UPDATE();
#endif

if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
fprintf(stderr, "DL TEST RINIT\n");
}

return SUCCESS;
}
/* }}} */

/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(dl_test)
{
if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
fprintf(stderr, "DL TEST RSHUTDOWN\n");
}

return SUCCESS;
}
/* }}} */
Expand Down Expand Up @@ -127,7 +150,7 @@ zend_module_entry dl_test_module_entry = {
PHP_MINIT(dl_test),
PHP_MSHUTDOWN(dl_test),
PHP_RINIT(dl_test),
NULL,
PHP_RSHUTDOWN(dl_test),
PHP_MINFO(dl_test),
PHP_DL_TEST_VERSION,
PHP_MODULE_GLOBALS(dl_test),
Expand Down
31 changes: 24 additions & 7 deletions sapi/fpm/fpm/fpm_php.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ static void fpm_php_disable(char *value, int (*zend_disable)(const char *, size_
}
/* }}} */

#define FPM_PHP_INI_ALTERING_ERROR -1
#define FPM_PHP_INI_APPLIED 1
#define FPM_PHP_INI_EXTENSION_FAILED 0
#define FPM_PHP_INI_EXTENSION_LOADED 2

int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode) /* {{{ */
{

Expand All @@ -90,45 +95,57 @@ int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode) /* {{{ */
zend_interned_strings_switch_storage(0);
php_dl(value, MODULE_PERSISTENT, &zv, 1);
zend_interned_strings_switch_storage(1);
return Z_TYPE(zv) == IS_TRUE;
return Z_TYPE(zv) == IS_TRUE ? FPM_PHP_INI_EXTENSION_LOADED : FPM_PHP_INI_EXTENSION_FAILED;
}

if (fpm_php_zend_ini_alter_master(name, name_len, value, value_len, mode, PHP_INI_STAGE_ACTIVATE) == FAILURE) {
return -1;
return FPM_PHP_INI_ALTERING_ERROR;
}

if (!strcmp(name, "disable_functions") && *value) {
zend_disable_functions(value);
return 1;
return FPM_PHP_INI_APPLIED;
}

if (!strcmp(name, "disable_classes") && *value) {
char *v = strdup(value);
PG(disable_classes) = v;
fpm_php_disable(v, zend_disable_class);
return 1;
return FPM_PHP_INI_APPLIED;
}

return 1;
return FPM_PHP_INI_APPLIED;
}
/* }}} */

static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp) /* {{{ */
{
struct key_value_s *kv;
int apply_result;
bool extension_loaded = false;

for (kv = wp->config->php_values; kv; kv = kv->next) {
if (fpm_php_apply_defines_ex(kv, ZEND_INI_USER) == -1) {
apply_result = fpm_php_apply_defines_ex(kv, ZEND_INI_USER);
if (apply_result == FPM_PHP_INI_ALTERING_ERROR) {
zlog(ZLOG_ERROR, "Unable to set php_value '%s'", kv->key);
} else if (apply_result == FPM_PHP_INI_EXTENSION_LOADED) {
extension_loaded = true;
}
}

for (kv = wp->config->php_admin_values; kv; kv = kv->next) {
if (fpm_php_apply_defines_ex(kv, ZEND_INI_SYSTEM) == -1) {
apply_result = fpm_php_apply_defines_ex(kv, ZEND_INI_SYSTEM);
if (apply_result == FPM_PHP_INI_ALTERING_ERROR) {
zlog(ZLOG_ERROR, "Unable to set php_admin_value '%s'", kv->key);
} else if (apply_result == FPM_PHP_INI_EXTENSION_LOADED) {
extension_loaded = true;
}
}

if (extension_loaded) {
zend_collect_module_handlers();
}

return 0;
}
/* }}} */
Expand Down
54 changes: 54 additions & 0 deletions sapi/fpm/tests/gh9921-php-value-ext-mod-handlers.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
FPM: GH-9921 - loading shared ext in FPM config does not register module handlers
--SKIPIF--
<?php
include "skipif.inc";
FPM\Tester::skipIfSharedExtensionNotFound('dl_test');
?>
--FILE--
<?php

require_once "tester.inc";

$cfg = <<<EOT
[global]
error_log = {{FILE:LOG}}
[unconfined]
listen = {{ADDR}}
pm = static
pm.max_children = 1
pm.status_path = /status
catch_workers_output = yes
env[PHP_DL_TEST_MODULE_DEBUG] = 1
php_admin_value[extension] = dl_test
EOT;

$code = <<<EOT
<?php
var_dump(extension_loaded('dl_test'));
EOT;

$tester = new FPM\Tester($cfg, $code);
$tester->start();
$tester->expectLogStartNotices();
$tester->request()->expectBody('bool(true)');
$tester->expectLogPattern('/DL TEST MINIT/');
$tester->expectLogPattern('/DL TEST RINIT/');
$tester->expectLogPattern('/DL TEST RSHUTDOWN/');
$tester->request()->expectBody('bool(true)');
$tester->expectLogPattern('/DL TEST RINIT/');
$tester->expectLogPattern('/DL TEST RSHUTDOWN/');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();

?>
Done
--EXPECT--
Done
--CLEAN--
<?php
require_once "tester.inc";
FPM\Tester::clean();
?>
<?php