Skip to content

Commit 8487d8f

Browse files
committed
Fix GH-9067: random extension is not thread safe
For thread-safety, we need to initialize global variables in GINIT (or RINIT), but not in MINIT. Closes GH-9070.
1 parent eac6330 commit 8487d8f

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.2.0beta2
44

5+
- Random:
6+
. Fixed bug GH-9067 (random extension is not thread safe). (cmb)
57

68
21 Jul 2022, PHP 8.2.0beta1
79

ext/random/random.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,35 @@ PHP_FUNCTION(random_int)
776776
}
777777
/* }}} */
778778

779+
/* {{{ PHP_GINIT_FUNCTION */
780+
static PHP_GINIT_FUNCTION(random)
781+
{
782+
random_globals->random_fd = -1;
783+
784+
random_globals->combined_lcg = php_random_status_alloc(&php_random_algo_combinedlcg, true);
785+
random_globals->combined_lcg_seeded = false;
786+
787+
random_globals->mt19937 = php_random_status_alloc(&php_random_algo_mt19937, true);
788+
random_globals->mt19937_seeded = false;
789+
}
790+
/* }}} */
791+
792+
/* {{{ PHP_GSHUTDOWN_FUNCTION */
793+
static PHP_GSHUTDOWN_FUNCTION(random)
794+
{
795+
if (random_globals->random_fd > 0) {
796+
close(random_globals->random_fd);
797+
random_globals->random_fd = -1;
798+
}
799+
800+
php_random_status_free(random_globals->combined_lcg, true);
801+
random_globals->combined_lcg = NULL;
802+
803+
php_random_status_free(random_globals->mt19937, true);
804+
random_globals->mt19937 = NULL;
805+
}
806+
/* }}} */
807+
779808
/* {{{ PHP_MINIT_FUNCTION */
780809
PHP_MINIT_FUNCTION(random)
781810
{
@@ -828,43 +857,13 @@ PHP_MINIT_FUNCTION(random)
828857
REGISTER_LONG_CONSTANT("MT_RAND_MT19937", MT_RAND_MT19937, CONST_CS | CONST_PERSISTENT);
829858
REGISTER_LONG_CONSTANT("MT_RAND_PHP", MT_RAND_PHP, CONST_CS | CONST_PERSISTENT);
830859

831-
RANDOM_G(random_fd) = -1;
832-
833-
RANDOM_G(combined_lcg) = php_random_status_alloc(&php_random_algo_combinedlcg, true);
834-
RANDOM_G(combined_lcg_seeded) = false;
835-
836-
RANDOM_G(mt19937) = php_random_status_alloc(&php_random_algo_mt19937, true);
837-
RANDOM_G(mt19937_seeded) = false;
838-
839-
return SUCCESS;
840-
}
841-
/* }}} */
842-
843-
/* {{{ PHP_MSHUTDOWN_FUNCTION */
844-
PHP_MSHUTDOWN_FUNCTION(random)
845-
{
846-
if (RANDOM_G(random_fd) > 0) {
847-
close(RANDOM_G(random_fd));
848-
RANDOM_G(random_fd) = -1;
849-
}
850-
851-
php_random_status_free(RANDOM_G(combined_lcg), true);
852-
RANDOM_G(combined_lcg) = NULL;
853-
854-
php_random_status_free(RANDOM_G(mt19937), true);
855-
RANDOM_G(mt19937) = NULL;
856-
857860
return SUCCESS;
858861
}
859862
/* }}} */
860863

861864
/* {{{ PHP_RINIT_FUNCTION */
862865
PHP_RINIT_FUNCTION(random)
863866
{
864-
#if defined(ZTS) && defined(COMPILE_DL_RANDOM)
865-
ZEND_TSRMLS_CACHE_UPDATE();
866-
#endif
867-
868867
RANDOM_G(combined_lcg_seeded) = false;
869868
RANDOM_G(mt19937_seeded) = false;
870869

@@ -878,14 +877,14 @@ zend_module_entry random_module_entry = {
878877
"random", /* Extension name */
879878
ext_functions, /* zend_function_entry */
880879
PHP_MINIT(random), /* PHP_MINIT - Module initialization */
881-
PHP_MSHUTDOWN(random), /* PHP_MSHUTDOWN - Module shutdown */
880+
NULL, /* PHP_MSHUTDOWN - Module shutdown */
882881
PHP_RINIT(random), /* PHP_RINIT - Request initialization */
883882
NULL, /* PHP_RSHUTDOWN - Request shutdown */
884883
NULL, /* PHP_MINFO - Module info */
885884
PHP_VERSION, /* Version */
886885
PHP_MODULE_GLOBALS(random), /* ZTS Module globals */
887-
NULL, /* PHP_GINIT - Global initialization */
888-
NULL, /* PHP_GSHUTDOWN - Global shutdown */
886+
PHP_GINIT(random), /* PHP_GINIT - Global initialization */
887+
PHP_GSHUTDOWN(random), /* PHP_GSHUTDOWN - Global shutdown */
889888
NULL, /* Post deactivate */
890889
STANDARD_MODULE_PROPERTIES_EX
891890
};

0 commit comments

Comments
 (0)